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

Script for data store saving doesnt work, spelling is correct (probably)?

Ima be honest, I DONT HAVE ANY IDEA WHY ITS NOT WORKING, I checked my spelling, could find anything wrong. If you have any idea what can be wrong, feel free to comment. also the script is in server script service and yes, I have API turned on

local DS = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "id_"..plr.userId
	local save1 = plr.leaderstats.Cash
	
	local GetSaved = DS:GetAsync(plrkey)
	if GetSaved then
		save1.Value = GetSaved[1]
	else
		local NumberForSaving = {save1.Value}
		DS:GetAsync(plrkey,NumberForSaving)
	end
end)

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

3 comments

0

Have you already created the leaderstats folder?

0

wot error say

0

What part doesn't work exactly?

1 answer

This is how you would implement it properly:

local DS = game:GetService("DataStoreService"):GetDataStore("SaveData")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = leaderstats
	
	local data -- We make an empty variable
	
	local success, errormessage = pcall(function() -- We should always wrap it in a pcall
		data = DS:GetAsync(plr.UserId) -- Now we load it
	end)
	
	if success and data then
		Cash.Value = data.Cash
	else
		warn("Error: "..tostring(errormessage))
	end
	
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success, errormessage = pcall(function()
		local data = {
			Cash = plr.leaderstats.Cash.Value;
		}
		
		DS:SetAsync(plr.UserId, data)
	end)
	
	if success then
		print("Data successfully saved for "..plr.Name)
	else
		warn("Error: "..tostring(errormessage))
	end
end)

Hope it helps.

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