Making these stats save/load Problems!

`I don’t understand what I’m doing wrong!

local DSS = game:GetService(“DataStoreService”)
local stats= game.ServerStorage.Stats
local datastore = DSS:GetDataStore(“GeneralSaveData”, “Players”)

function generateDataKey(plr)
local ret = “uid_” … plr.userId
return ret
end

function generateDataTable(plr)
local dataTable = {
Durability = stats:FindFirstChild(plr.Name).Durability.Value,
HandleSize = stats:FindFirstChild(plr.Name).HandleSize.Value,
Height = stats:FindFirstChild(plr.Name).Height.Value,
Num = stats:FindFirstChild(plr.Name).Num.Value,
Size = stats:FindFirstChild(plr.Name).Size.Value,
Speed = stats:FindFirstChild(plr.Name).Speed.Value,
Strength = stats:FindFirstChild(plr.Name).Strength.Value
}
return dataTable
end

function saveDataForPlr(plr)
local key = generateDataKey(plr)
local data = generateDataTable(plr)
datastore:SetAsync(key, data)
end

function inputDataToPlayer(plr, data)
stats:FindFirstChild(plr.Name).Durability.Value = data.Durability
stats:FindFirstChild(plr.Name).HandleSize.Value = data.HandleSize
stats:FindFirstChild(plr.Name).Height.Value = data.Height
stats:FindFirstChild(plr.Name).Num.Value = data.Num
stats:FindFirstChild(plr.Name).Size.Value = data.Size
stats:FindFirstChild(plr.Name).Speed.Value = data.Speed
stats:FindFirstChild(plr.Name).Strength.Value = data.Strength
end

function loadDataForPlr(plr)
local key = generateDataKey(plr)
local data = datastore:GetAsync(key)
inputDataToPlayer(plr, data)
end

game.Players.PlayerAdded:connect(function(plr)
loadDataForPlr(plr) --Load first thing when they join
stats:FindFirstChild(plr.Name).Durability.Changed:connect(function()
saveDataForPlr(plr)
end)
stats:FindFirstChild(plr.Name).HandleSize.Changed:connect(function()
saveDataForPlr(plr)
end)
stats:FindFirstChild(plr.Name).Height.Changed:connect(function()
saveDataForPlr(plr)
end)
stats:FindFirstChild(plr.Name).Num.Changed:connect(function()
saveDataForPlr(plr)
end)
stats:FindFirstChild(plr.Name).Size.Changed:connect(function()
saveDataForPlr(plr)
end)
stats:FindFirstChild(plr.Name).Speed.Changed:connect(function()
saveDataForPlr(plr)
end)
stats:FindFirstChild(plr.Name).Strength.Changed:connect(function()
saveDataForPlr(plr)
end)
end)

game.Players.PlayerRemoving:connect(saveDataForPlr)`

What is the issue specifically? Any output errors or warnings? You can’t paste a bunch of code and expect us to know what is wrong.

2 Likes

You should not be saving every time the values change. For now, make it save on PlayerRemoving and BindToClose instead.

2 Likes

Sorry for that.

No output errors and it’s for a weight lifting game do you need the server script service stats script to see the issue?

How would I do that sorry I am a visual learner I do understand it’s a bunch of code but I’ve been trying for long time to save and load these stats into player on join. but all fail!

You should probably use :WaitForChild on all of the children as one of the scripts may execute faster than the other.

You should also switch from :connect to :Connect as :connect is deprecated.

1 Like

Wait, where do you even make these values? Is it in a separate script? I do not see where you declare these instances.

Also, by the way:

game.Players.PlayerRemoving:Connect(function(plr)
   --// Save here
end)

Ok so where would I declare that in at?

I’m confused, what do you mean?

I mean I’m confused on why it don’t update the values in gui on join maybe the problem is in my local idk man I been trying…

There is not leaderstats just stats folder in server storage

The client can not access ServerStorage. You can either make a folder that the client can access so they can have .Changed functions, or you can fire the client with the updated values when it changes. It’s up to you and your structure.

Ok so if I make a folder an put it in the client can’t they alter those values?

On their client, yes, but it wouldn’t replicate to the server.

1 Like

Ok thanks I will try that now!

Good luck with that! If you have any more issues then just tell me, although I may need to go soon.

1 Like