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

Did I make a mistake rewriting leaderstats code?

I got the code on leaderstats and for saving it from Harp Seal user. I rewritten it and** it doesn't work**. I have API enabled. I don't know what I could have done wrong.

Here is "Data" script:

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

game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "id_"..plr.UserId
	local saves = plr.leaderstats.Money
	local Save = ds:GetAsync(plrkey)
	
	if Save then
		saves.Value = Save[1]
	else 
		local numbersave  = {saves.Value}
		ds:GetAsync(plrkey, numbersave)
		
	end

end)

game.Players.PlayerRemoving:Connect(function(plr)
	ds:GetAsync("id"..plr.UserID, {plr.leaderstats.Money.Value})
end)

And here is save script

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

game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "id_"..plr.UserId
	local saves = plr.leaderstats.Money
	local Save = ds:GetAsync(plrkey)
	
	if Save then
		saves.Value = Save[1]
	else 
		local numbersave  = {saves.Value}
		ds:GetAsync(plrkey, numbersave)
		
	end

end)

game.Players.PlayerRemoving:Connect(function(plr)
	ds:GetAsync("id"..plr.UserID, {plr.leaderstats.Money.Value})
end)

Nwm I heard that they have it on the dev forum, can someone compare it or something like that

1 answer

✓ Accepted answer

I'll help you

Make a script in ServerScriptService only one

Call it whatever you want

Use this code

local dataStoreService = game:GetService("DataStoreService")

local dataStore = dataStoreService:GetDataStore("DataStore") -- Name it whatever you want

local playersLeft = 0

game:GetService("Players").PlayerAdded:Connect(function(player)
	
	playersLeft += 1
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local coins = Instance.new("IntValue")
	coins.Name = "Coins" -- Name it whatever you want
	coins.Value = 0
	coins.Parent = leaderstats
	
	local coins_data
	
	pcall(function()
		coins_data = dataStore:GetAsync(player.UserId.."-coins")
	end)
	
	if coins_data ~= nil then
		
		coins.Value = coins_data
	else
		print("Player has no coins!")
	end
	
end)

local bindableEvent = Instance.new("BindableEvent")

game:GetService("Players").PlayerRemoving:Connect(function(player)
	
	pcall(function()
		dataStore:SetAsync(player.UserId.."-coins", player.leaderstats.Coins.Value) -- change Coins to what the name of your stat is called
	end)
	
	playersLeft -= 1
	bindableEvent:Fire()
	print("Saved!")
	
end)

game:BindToClose(function()
	
	while playersLeft > 0 do
		bindableEvent.Event:Wait()
		wait()
	end
end)

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