Leader stats saving help!

So I have this script:

local BOOK = game.StarterGui.UI.BOOK
local IQ = game.StarterGui.UI.IQ
local VSIZE = game.StarterGui.UI.VSIZE
local COINTEXT = game.StarterGui.UI.COINS

game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new(“Folder”) – Don’t Change
leaderstats.Name = “leaderstats” – Don’t Change
leaderstats.Parent = Player – Don’t Change

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

local Value2 = Instance.new("IntValue")
Value2.Name = "Coins"
Value2.Parent = leaderstats
Value2.Value= 0

game.ReplicatedStorage.HEADINCREASESTAT.OnServerEvent:Connect(function()
	print("CLICK")
	Value.Value = Value.Value + BOOK.Value
end)

game.ReplicatedStorage.SELLSTAT.OnServerEvent:Connect(function()
	print("SELL")
	Value2.Value = Value2.Value + Value.Value * IQ.Value	
	Value.Value = 0
end)

end)

I made it so that it creates 2leader stats, one for coins and one for size.Every time the player clicks a size value changes and when it does the leaderstat is updated to show that size, same thing with coins but its when a player touches a part.

So my question is, what would be the easiest way to save the data and then load it in?

And when I load it in I want value in the starter GUI to change to that leaderstat.

Thanks!

4 Likes

I have just found a problem where the leader stat is linked to other players,so every time a stat changes everyones stat changes

Best method is using DataStoreService to save the data, now if you want a gui to save the currency its easy, if the currency saves and the gui is scripted, the gui will say everytime the current currency of player without using any data.

Gui:

local PlayerStats = game.Players.LocalPlayer:WaitForChild("leaderstats")

local Gui = script.Parent

PlayerStats.Currency:GetPropertyChangedSignal("Value"):Connect(function() -- Change Currency to your stat data

Gui.Text = PlayerStats.Currency -- Change Currency to your stat data (do the same)

end)

Another error i see, dont fire events as server, because it will fire for all.

1 Like

Will that always set my playerstat to the Gui?

Yes, this is how your player stat will look with saving data and fixed.

local DataStoreService = game:GetService("DataStoreService")
local CheetahData = DataStoreService:GetDataStore("Cheetah")

game.Players.PlayerAdded:Connect(function(Player)
	local PlayerId = "Id-"..Player.UserId
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats" 
	leaderstats.Parent = Player
	
	local Value = Instance.new("IntValue")
	Value.Name = "Size"
	Value.Parent = leaderstats
	Value.Value = 0
	
	local Value2 = Instance.new("IntValue")
	Value2.Name = "Coins"
	Value2.Parent = leaderstats
	Value2.Value = 0
	
	local GetSaved = CheetahData:GetAsync(PlayerId)
	local Saved,Error = pcall(function()
		Value.Value = GetSaved[1]
		Value2.Value = GetSaved[2]
	end)
	if not Saved then
		print(tostring(Error))
		local Saving1 = {Value.Value}
		local Saving2 = {Value2.Value}
		CheetahData:SetAsync(PlayerId,Saving1,Saving2)
	end
	
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local PlayerId = "Id-"..Player.UserId
	local Save1 = {Player.leaderstats.Size.Value}
	local Save2 = {Player.leaderstats.Coins.Value}
	CheetahData:SetAsync(PlayerId,Save1,Save2)
end)
3 Likes

Hey,so i replaced my script with that then run the game,I changed the leaderstat value from the explorer and then left the game and came back but it didn’t save my data,why is that?

No, thats totally wrong, change the value on Client, or just do some script to change the value.

Thanks alot that worked perfectly!

No problem bro! Good luck with your game!

1 Like

The only thing I would change about EncodedCheetah’s script is to localize the datastore inside the PlayerAdded function. Then write it like so: local CheetahData = game:GetService(“DatastoreService”):GetDatastore(“Save”…Player.UserId)

That way the datastore doesn’t get bombarded with requests.

2 Likes

Oh, my bad, i totally agree with you, thank you for telling me

Hey,thanks for the reply.
How would I localise the data store?

Localizing basically means your defining what something is. So put that line of code inside the PlayerAdded function instead of out of it.

hey,Sorry for the really late reply,what do I replace your code with? I tried to replace it with local PlayerId = “Id-”…Player.UserId but that got me a error

local DataStoreService = game:GetService("DataStoreService")

game.Players.PlayerAdded:Connect(function(Player)
local CheetahData = DataStoreService:GetDataStore("Cheetah"..Player.UserId)
local PlayerId = "Id-"..Player.UserId
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats" 
	leaderstats.Parent = Player
	
	local Value = Instance.new("IntValue")
	Value.Name = "Size"
	Value.Parent = leaderstats
	Value.Value = 0
	
	local Value2 = Instance.new("IntValue")
	Value2.Name = "Coins"
	Value2.Parent = leaderstats
	Value2.Value = 0
	
	local GetSaved = CheetahData:GetAsync(PlayerId)
	local Saved,Error = pcall(function()
		Value.Value = GetSaved[1]
		Value2.Value = GetSaved[2]
	end)
	if not Saved then
		print(tostring(Error))
		local Saving1 = {Value.Value}
		local Saving2 = {Value2.Value}
		CheetahData:SetAsync(PlayerId,Saving1,Saving2)
	end
	
end)

game.Players.PlayerRemoving:Connect(function(Player)
local CheetahData = DataStoreService:GetDataStore("Cheetah"..plr.UserId)
local PlayerId = "Id-"..Player.UserId
	local Save1 = {Player.leaderstats.Size.Value}
	local Save2 = {Player.leaderstats.Coins.Value}
	CheetahData:SetAsync(PlayerId,Save1,Save2)
end)
2 Likes

Hey,thanks for the reply I tried that and it said I had to make local CheetahData = DataStoreService:GetDataStore(“Cheetah”…Player.UserId) public.

Turn on API and don’t use three dots. Only use two.

1 Like