DataStore Help (Not saving)

My DataStore Is not saving TIME.

local ds = game:GetService("DataStoreService"):GetDataStore("Save Data")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "id_"..plr.userId
	local save1 = plr.leaderstats.TIME
	local save2 = plr.leaderstats.Flex_Point
	
	local GetSaved = ds.GetAsync(plrkey)
	if GetSaved then
		save1.Value = GetSaved[1]
		save2.Value = GetSaved[2]
	else
		local NumberForSaving = {save1.Value,save2.Value}
		ds:GetAsync(plrkey,NumberForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	ds:SetAsync("id_"..plr.userId, {plr.leaderstats.TIME.Value, plr.leaderstats.Flex_Points.Value})
end)

Used this tutorial

1 Like

The pain of not using pcalls to properly call your DataStore functions :pensive:

You should always encase your DataStore functions in pcalls, so that the Data will be able to securely be saved & loaded upon joining/leaving the game

local ds = game:GetService("DataStoreService"):GetDataStore("Save Data")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "id_"..plr.userId
	local save1 = plr.leaderstats.TIME
	local save2 = plr.leaderstats.Flex_Point
	
    local data
    local success, whoops = pcall(function()
        data = ds:GetAsync(plrkey)
    end)

    if success and data then
        save1.Value = data[1]
        save2.Value = data[2]
        print("Loaded data to: ", plr.Name)
    else
        warn("This user has no data or an error has occured, more info: ", whoops)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local success, whoops = pcall(function()
    	ds:SetAsync("id_"..plr.userId, {plr.leaderstats.TIME.Value, plr.leaderstats.Flex_Points.Value})
    end)

    if success then
        print("Data saved successfully!")
    else
        warn("An error occurred saving the data, more info: ", whoops)
    end
end)

Still did not work.

It used to work. But someone edited it and I remade and not it doesnt

I forgot to call the GetAsync function aaa

If it still doesn’t work, are there any print statements that are outputting?

No just the warn statment was there

Which warn statement exactly? The GetAsync or the SetAsync function?

This warn one is the one that had poped up.

Look at the video some more.

Found some errors but it still doesnt work.

Could someone use the video and check my script.

local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "id_"..plr.userId
	local save1 = plr.leaderstats.TIME
	local save2 = plr.leaderstats.Flex_Points

	local GetSaved = ds.GetAsync(plrkey)
	if GetSaved then
		save1.Value = GetSaved[1]
		save2.Value = GetSaved[2]
	else
		local NumberForSaving = {save1.Value, save2.Value}
		ds:GetAsync(plrkey, NumberForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	ds:SetAsync("id_"..plr.userId, {plr.leaderstats.TIME.Value, plr.leaderstats.Flex_Points.Value})
end)

Ok time to remake your code again >:(

local ds = game:GetService("DataStoreService"):GetDataStore("Save Data")

game.Players.PlayerAdded:Connect(function(plr)
    local plrkey = "id_"..plr.UserId
    local save1 = plr:WaitForChild("leaderstats").TIME
    local save2 = plr:WaitForChild("leaderstats").Flex_Point

    local data
    local success, whoops = pcall(function()
        data = ds:GetAsync(plrkey)
    end)

    print(data)
    if success and data then
        save1.Value = data.Time
        save2.Value = data.Flex
        print("Loaded data to: ", plr.Name)
    else
        warn("This user has no data or an error has occured, more info: ", whoops)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local data = {
        Time = plr.leaderstats.TIME.Value,
        Flex = plr.leaderstats.Flex_Point.Value
}
    local success, whoops = pcall(function()
    	ds:SetAsync("id_"..plr.userId, data)
    end)

    if success then
        print("Data saved successfully!")
    else
        warn("An error occurred saving the data, more info: ", whoops)
    end
end)

Try changing the value on the server side, if you’re changing it from the client side it won’t be able to work that way

Also do please look at what the Output error is when it prints

1 Like

What does this mean exacltlyy? Try in game not studio?

LETSSS GOOOOOOOOOOOOOOOO

Thank you so much

It works.

Made a few minor tweeks but it works.

1 Like

Oh thank heavens I thought we had to spend 500 hours on this topic

Also for this:

I meant like the game-testing modes you can choose from

You can change the stats manually if you’d prefer to test it that way on the Server (Right) side

1 Like

Oh I see.

I sat in game for about 5 min just getting the Leaderstat up lol