Alright, after a lot of time and hard work I got how to do a decent load / save data script. 1st Try.
local DataStore = game:GetService("DataStoreService"):GetDataStore("StoreName") game.Players.PlayerAdded:connect(function(player) local key = "key-"..player.userId local points = game.ReplicatedStorage.AvailablePoints local strength = game.ReplicatedStorage.Strength local level = game.ReplicatedStorage.Level local quirk = game.ReplicatedStorage.Quirk local save = DataStore:GetAsync(key) if save then points.Value = save[1] strength.Value = save[2] level.Value = save[3] quirk.Value = save[4] else local load = {points.Value, strength.Value, quirk.Value, level.Value} DataStore:SetAsync(key,load) end end) game.Players.PlayerRemoving:connect(function(player) local key = "key-"..player.userId local load = {game.ReplicatedStorage:FindFirstChild("AvailablePoints").Value, game.ReplicatedStorage:FindFirstChild("Quirk").Value, game.ReplicatedStorage:FindFirstChild("Level").Value, game.ReplicatedStorage:FindFirstChild("Strength").Value} DataStore:SetAsync(key,load) end)
And it actually worked! but, the real problem now is. It only loads me the points
value, and not also strength, quirk and level. Can you find any error in here? I also tried to give myself more Points with a script and tried different times to give me points in Strength since only AvailablePoints loaded and not the others!
EDIT: This is the actual script that keeps not working.
local DataStore = game:GetService("DataStoreService") local ST = DataStore:GetDataStore("StoreName") game.Players.PlayerAdded:Connect(function(player) local Backpack = player:WaitForChild("Backpack") local points = Backpack.AvailablePoints local strength = Backpack.Strength local level = Backpack.Level local quirk = Backpack.Quirk points.Value = ST:GetAsync(player.UserId) or 1 strength.Value = ST:GetAsync(player.UserId) or 2 level.Value = ST:GetAsync(player.UserId) or 3 quirk.Value = ST:GetAsync(player.UserId) or 4 while wait(7) do ST:SetAsync(player.UserId, points.Value, strength.Value, level.Value, quirk.Value) print("works!") end end) game.Players.PlayerRemoving:Connect(function(player) local Backpack = player:WaitForChild("Backpack") ST:SetAsync(player.UserId, Backpack.AvailablePoints.Value, Backpack.Strength.Value, Backpack.Level.Value, Backpack.Quirk.Value) print("works!") end)
Alright so i rewrote your script and to my knowledge it work, contact me if this does not work
local DataStore = game:GetService("DataStoreService") local ST = DataStore:GetDataStore("StoreName") game.Players.PlayerAdded:Connect(function(player) local points = game.ReplicatedStorage.AvaliablePoints points.Value = ST:GetAsync(player.UserId) or 1 --local strength = game.ReplicatedStorage.Strength --local level = game.ReplicatedStorage.Level --local quirk = game.ReplicatedStorage.Quirk points.Changed:Connect(function() ST:SetAsync(player.UserId, points.Value) print("works!") end) end) game.Players.PlayerRemoving:Connect(function(player) ST:SetAsync(player.UserId, game.ReplicatedStorage.AvaliablePoints.Value) print("works!") end)
So do the same thing i did to the points to all the rest of the values you have. Be sure to also turn on API serviced in the game settings
Hope this helps, Father_Odin
I've tried this, but still not working. It saves everything, but when I come in game, Strength value is wrong, but Available points is correct. Why?
local DataStore = game:GetService("DataStoreService") local ST = DataStore:GetDataStore("StoreName") game.Players.PlayerAdded:Connect(function(player) local Backpack = player:WaitForChild("Backpack") local points = Backpack.AvailablePoints local strength = Backpack.Strength local level = Backpack.Level local quirk = Backpack.Quirk points.Value = ST:GetAsync(player.UserId) or 1 strength.Value = ST:GetAsync(player.UserId) or 2 level.Value = ST:GetAsync(player.UserId) or 3 quirk.Value = ST:GetAsync(player.UserId) or 4 while wait(7) do ST:SetAsync(player.UserId, points.Value, strength.Value, level.Value, quirk.Value) print("works!") end end) game.Players.PlayerRemoving:Connect(function(player) local Backpack = player:WaitForChild("Backpack") ST:SetAsync(player.UserId, Backpack.AvailablePoints.Value, Backpack.Strength.Value, Backpack.Level.Value, Backpack.Quirk.Value) print("works!") end)
You want to be creating new stats per player but in your code you use the same value holders to store stats meaning you are mixing player data.
To simplify thing you can create them when the plauer joins using a simple function to make the instance.
Example:-
-- create a new instance with the data passed local function mkStat(statType, name, val) local tmp = Instance.new(statType) tmp.Name = name if val ~= nil then -- set value if one is passed tmp.Value = val end return tmp end game.Players.PlayerAdded:Connect(function(plr) -- player stats local points = mkStat("IntValue", "Points", 0) local level = mkStat("IntValue", "Level", 0) -- load data in points.Parent = plr level.Parent = plr end)
This will create a new IntValue per player which then we can load / save data from.
Example:-
local statsDS = game:GetService("DataStoreService"):GetDataStore("StoreName") local prefix = "usr_" -- local functions local function mkStat(statType, name, val) local tmp = Instance.new(statType) tmp.Name = name if val ~= nil then tmp.Value = val end return tmp end game.Players.PlayerAdded:Connect(function(plr) local points = mkStat("IntValue", "Points", 0) local level = mkStat("IntValue", "Level", 0) -- load data in local data = ST:GetAsync(prefix .. tostring(plr.UserId)) if data then print("Data loaded for " .. prefix .. tostring(plr.UserId)) points.Value = data[1] level.Value = data[2] end points.Parent = plr level.Parent = plr end) game.Players.PlayerRemoving:Connect(function(plr) local data = { plr:WaitForChild("Points").Value, plr:WaitForChild("Level").Value } -- pass a table to save ST:SetAsync(prefix .. tostring(plr.UserId), data) end)
This is just a basic example and does not handle any data store errors so loss of player data is possible. I would also recommend against using a array of data for loading / saving as if one value is not present then others will be out of order.
Instead use a dictionary i.e.
local data = {} for name, ins in pairs(plr:GetChildren()) do data[name] = ins.Value end
But this will mean that you would need to change how you load data in.
I hope this helps