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.
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.
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)
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?
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.
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)