Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why won't my DataStore load or save?

It prints Data loaded and Data Saved when entering or leaving the game, but it doesn't actually save. When I join the game it still says 0.

local Datastore = game:GetService("DataStoreService"):GetDataStore('Rocky-Test91')

game.Players.PlayerAdded:Connect(function(player)
	local Key = 'Player-ID:' .. player.userId
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = 'leaderstats'
	leaderstats.Parent = player
	
	local Pebbles = Instance.new("IntValue")
	Pebbles.Name = 'Pebbles'
	Pebbles.Value = 0
	Pebbles.Parent = leaderstats
	
	local GetSave = Datastore:GetAsync(Key)
	if GetSave then
		Pebbles.Value = GetSave
		print('Data Loaded for '..player.Name)
	else
		local Numbers = {Pebbles.Value}
		Datastore:SetAsync(Key, Numbers)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local Key = 'PlayerID:' .. player.userId
	local ValueToSave = {player.leaderstats.Pebbles.Value}
	Datastore:SetAsync(Key, ValueToSave)
	print('Data Saved For '..player.Name)
end)

2 comments

0

Instead of "local Key = 'PlayerID:' .. player.userId" just do "local Key = player.userId"

0

That won't work. Datastores only accept strings as keys. One thing to note: It's "player.UserId" not "player.userId."

1 answer

✓ Accepted answer

You set ValueToSave as a table, i'm not sure why but since it's a table all you're doing to calling the table itself and not the values inside it. Also it's good practice to use a BindToClose function at the end so the data will be saved when the server is about to shut down.

local Datastore = game:GetService("DataStoreService"):GetDataStore('Rocky-Test91')

game.Players.PlayerAdded:Connect(function(player)
    local Key = 'Player-ID:' .. player.userId
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = 'leaderstats'
    leaderstats.Parent = player

    local Pebbles = Instance.new("IntValue")
    Pebbles.Name = 'Pebbles'
    Pebbles.Value = 0
    Pebbles.Parent = leaderstats

    local GetSave = Datastore:GetAsync(Key)
    if GetSave then
        Pebbles.Value = GetSave
        print('Data Loaded for '..player.Name)
    else
        local Numbers = {Pebbles.Value}
        Datastore:SetAsync(Key, Numbers)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local Key = 'PlayerID:' .. player.userId
    local ValueToSave = {player.leaderstats.Pebbles.Value}
    Datastore:SetAsync(Key, ValueToSave[1]) -- calls the value from the table
    print('Data Saved For '..player.Name)
end)

	game:BindToClose(function()
		for i,v in pairs(game.Players:GetPlayers()) do
			if v then
			v:Kick() -- kicks all players before server shuts down so their data will save from the PlayerRemoving event
		end
	end
	wait(3) -- waits 3 seconds before the server shuts down so the player's data will save
end)

1 comment

0

Thanks, it worked.

This discussion is preserved as it was. New answers, comments, and votes are closed.