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

Saving Leaderboard Script not working?

So the script im using to make and save and load the leaderboard + Money is not working and i have spend way to much time trying to fix it and it still doesn't work.

The Script:

local DataStoreService = game:GetService("DataStoreService")
local MoneyDataStore = DataStoreService:GetDataStore("MoneyStats")

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderboard = Instance.new("Folder", Player)
	Leaderboard.Name = "Leaderboard"
	
	local Money = Instance.new("IntValue", Leaderboard)
	Money.Name = "Money"
	
	local Data = MoneyDataStore:GetAsync(Player.UserId)
	if Data then
		Money.Value = Data
	end	
end)

game.Players.PlayerRemoving:Connect(function(Player)
	MoneyDataStore:SetAsync(Player.UserId, {
		["Money"] = Player.Leaderboard.Money.Value;
	})
end)

2 comments

0

Just use setasync like this: MoneyDataStore:SetAsync(Player.UserId, Player.Leaderboard.Money.Value)

0

And you should use pcall. They are important.

1 answer

✓ Accepted answer

I think this might help, I did not test it yet, I dont think there are any bugs.

local DataStoreService = game:GetService("DataStoreService")
local MoneyDataStore = DataStoreService:GetDataStore("MoneyStats")
--------------------------------------scripts------------------------
game.Players.PlayerAdded:Connect(function(Player)
	local Leaderboard = Instance.new("Folder", Player)
	Leaderboard.Name = "Leaderboard"

	local Money = Instance.new("IntValue", Leaderboard)
	Money.Name = "Money"
---------------------------------------money loader-----------------------------	
	pcall(function()
		local moneyVal = MoneyDataStore:GetAsync(Player.UserID) --getting Player's data from user id
		if moneyVal then 
		moneyVal.Value = moneyVal -- changing the player's money to saved money
		end
	end)		
end)
--------------------------------------money saver-------------------------
game.Players.PlayerRemoving:Connect(function(Player)
	pcall(function()
		if Player:FindFirstChild("leaderstats") and Player.leaderstats:FindFirstChild("Cash")and Player.leaderstats.Cash.Value > 0 then --conditions to save data
			MoneyDataStore:SetAsync(Player.UserID,Player.leaderstats.Money.Value)--saving data
		end
	end)
end)

2 comments

0

Data Store Doesnt Save On Studio.

0

I hate to break it to you but it does not work

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