tech/game

21/08/02 Roblox Studio Tutorial: How to Save Data

tech-lover 2021. 8. 2. 13:38

https://youtu.be/-wAJAI95ivs

 

References

https://developer.roblox.com/en-us/articles/Data-store

 

Data Stores

This Platform uses cookies to offer you a better experience, to personalize content, to provide social media features and to analyse the traffic on our site. For further information, including information on how to prevent or manage the use of cookies on t

developer.roblox.com

https://developer.roblox.com/en-us/onboarding/intro-to-saving-data

 

Introduction to Saving Data

This Platform uses cookies to offer you a better experience, to personalize content, to provide social media features and to analyse the traffic on our site. For further information, including information on how to prevent or manage the use of cookies on t

developer.roblox.com

- ServerScriptService

```

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("PlayerData")

 

local function onPlayerJoin(player)  -- Runs when players join

    local leaderstats = Instance.new("Folder")  --Sets up leaderstats folder

    leaderstats.Name = "leaderstats"

    leaderstats.Parent = player

 

    local gold = Instance.new("IntValue"--Sets up value for leaderstats

    gold.Name = "Gold"

    gold.Parent = leaderstats

 

    local playerUserId = "Player_" .. player.UserId  --Gets player ID

    local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data

    if data then

        -- Data exists for this player

        gold.Value = data

    else

        -- Data store is working, but no current data for this player

        gold.Value = 0

    end

end

 

local function onPlayerExit(player)  --Runs when players exit

    local success, err = pcall(function()

        local playerUserId = "Player_" .. player.UserId

        playerData:SetAsync(playerUserId, player.leaderstats.Gold.Value--Saves player data

    end)

 

    if not success then

        warn('Could not save data!')

    end

end

 

game.Players.PlayerAdded:Connect(onPlayerJoin)

game.Players.PlayerRemoving:Connect(onPlayerExit)

```

 

-- Storing More Values

```

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("PlayerData")

 

local function onPlayerJoin(player)  -- Runs when players join

    local leaderstats = Instance.new("Folder")  --Sets up leaderstats folder

    leaderstats.Name = "leaderstats"

    leaderstats.Parent = player

 

    local money = Instance.new("IntValue"--Sets up value for leaderstats

    money.Name = "Money"

    money.Parent = leaderstats

 

    local exp = Instance.new("IntValue"--Sets up value for leaderstats

    exp.Name = "Experience"

    exp.Parent = leaderstats

 

    local playerUserId = "Player_" .. player.UserId  --Gets player ID

    local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data

    if data then

        money.Value = data['Money']

        exp.Value = data['Experience']

    else

        -- Data store is working, but no current data for this player

        money.Value = 0

        exp.Value = 0

    end

end

 

local function create_table(player)

    local player_stats = {}

    for _, stat in pairs(player.leaderstats:GetChildren()) do

        player_stats[stat.Name] = stat.Value

    end

    return player_stats

end

 

local function onPlayerExit(player)  --Runs when players exit

    local player_stats = create_table(player)

    local success, err = pcall(function()

        local playerUserId = "Player_" .. player.UserId

        playerData:SetAsync(playerUserId, player_stats) --Saves player data

    end)

    if not success then

        warn('Could not save data!')

    end

end

 

game.Players.PlayerAdded:Connect(onPlayerJoin)

game.Players.PlayerRemoving:Connect(onPlayerExit)

```