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

Data store for a leaderstat does not work?

Hello, I am working on a game that uses a data store script to save the amount of currency you have but for some reason it does not work.

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("ToadsSaver")

game.Players.PlayerAdded:connect(function(player)
	local leader = Instance.new("Folder",player)
	leader.Name = "leaderstats"
	local Toads = Instance.new("IntValue",leader)
	Toads.Name = "Toads"
	Toads.Value = ds:GetAsync(player.UserId) or 0
	ds:SetAsync(player.UserId, Toads.Value)
	Toads.Changed:connect(function()
		ds:SetAsync(player.UserId, Toads.Value
	end)
end)


game.Players.PlayerRemoving:connect(function(player)
	ds.SetAsync(player.UserId, player.leaderstats.Toads.Value)
end)

1 answer

This code will work!

--[[Savin'
		Dem
			Stats	
--]]
game.Players.PlayerRemoving:connect(function(player)
	local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")

local statstorage = player:FindFirstChild("leaderstats"):GetChildren()
for i =  1, #statstorage do
	datastore:SetAsync(statstorage[i].Name, statstorage[i].Value)
	print("saved data number "..i)
	
end
print("Stats successfully saved")	
end)


--[[
	Loadin'
		Dem
			Stats
--]]
game.Players.PlayerAdded:connect(function(player)
		local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")
	
	player:WaitForChild("leaderstats")
	wait(1)
	local stats = player:FindFirstChild("leaderstats"):GetChildren()
	for i = 1, #stats do			
	stats[i].Value = datastore:GetAsync(stats[i].Name)
	print("stat numba "..i.." has been found")
		end
end)

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