Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Datastore data isn't saving when the player leaves? [closed]

Asked by
DemGame 271 Moderation Voter
3 years ago

I have recently watched a tutorial on how to use the datastore service on roblox, but it doesn't really save correctly. (It prints the error message on that it failed to save when a player leaves.)

Here's my code:

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
    local stats = Instance.new("Folder")
    stats.Name = "stats"
    stats.Parent = player

    local valuetwo = Instance.new("BoolValue")
    valuetwo.Name = "two"
    valuetwo.Parent = stats
    local valuethree = Instance.new("BoolValue")
    valuethree.Name = "three"
    valuethree.Parent = stats
    local valuefour = Instance.new("BoolValue")
    valuefour.Name = "four"
    valuefour.Parent = stats

    local data2
    local data3
    local data4
    local success, errormessage = pcall(function()
        data2 = myDataStore:GetAsync(player.UserId.."-two")
        data3 = myDataStore:GetAsync(player.UserId.."-three")
        data4 = myDataStore:GetAsync(player.UserId.."-four")
    end)

    if success then
        valuetwo.Value = data2
        valuethree.Value = data3
        valuefour.Value = data4
    else
        print("data failed to load")
        warn(errormessage)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)

    local success, errormessage = pcall(function()
        myDataStore:SetAsync(player.UserId.."-two",player.stats.valuetwo.Value)
        myDataStore:SetAsync(player.UserId.."-three",player.stats.valuethree.Value)
        myDataStore:SetAsync(player.UserId.."-four",player.stats.valuefour.Value)   
    end)

    if success then
        print("player data saved")
    else
        print("oh no data save failed")
        warn(errormessage)
    end
end)

Closed as Non-Descriptive by JesseSong

This question has been closed because its title or content does not adequately describe the problem you are trying to solve.
Please ensure that your question pertains to your actual problem, rather than your attempted solution. That is, you were trying to solve problem X, and you thought solution Y would work, but instead of asking about X when you ran into trouble, you asked about Y.

Why was this question closed?