Hey guys could you help me out with my data saving leaderstats it not work on my friends place but it works on my place
local Players = game:GetService(“Players”)
local DSS = game:GetService(“DataStoreService”)
local Data1 = DSS:GetDataStore(“Data”)
local RS = game:GetService(“RunService”)
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Cash = Instance.new("IntValue", leaderstats)
Cash.Name = "Cash"
local Strengh = Instance.new("IntValue", leaderstats)
Strengh.Name = "Strengh"
local GripStrengh = Instance.new("IntValue", leaderstats)
GripStrengh.Name = "GripStrengh"
local Speed = Instance.new("IntValue", leaderstats)
Speed.Name = "Speed"
local PlayerData
local Success, ErrorMsg = pcall(function()
local PlayerKey = player.UserId
PlayerData = Data1:GetAsync(PlayerKey)
end)
if Success then
if PlayerData then
Cash.Value = PlayerData[1]
Strengh.Value = PlayerData[2]
GripStrengh.Value = PlayerData[3]
Speed.Value = PlayerData[4]
end
else
warn(ErrorMsg)
end
end)
local function SaveData(player)
local user = player
local leaderstats = user:WaitForChild(“leaderstats”)
if leaderstats then
print(“leaderstats”)
end
local PlayerData = {
leaderstats.Cash.Value,
leaderstats.GripStrengh.Value,
leaderstats.Strengh.Value,
leaderstats.Speed.Value
}
local PlayerKey = player.UserId
local Success, ErrorMsg = pcall(function()
Data1:SetAsync(PlayerKey, PlayerData)
end)
if not Success then
warn(ErrorMsg)
end
Saving is not an issue in Studio. If you Hold down the Stop button it saves data when your session ends, if you click it once, it typically will not save anything to DataStores. This may be the case with other features in Studio.
(While this isn’t confirmed anywhere, it’s a pattern I’ve noticed after many experiences).
I made a few small changes. Also, it seems like some of the curly quotation marks could be causing an error, so I replaced those just in case.
local Players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
local Data1 = DSS:GetDataStore("Data")
local RS = game:GetService("RunService")
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Cash = Instance.new("IntValue", leaderstats)
Cash.Name = "Cash"
local Strengh = Instance.new("IntValue", leaderstats)
Strengh.Name = "Strengh"
local GripStrengh = Instance.new("IntValue", leaderstats)
GripStrengh.Name = "GripStrengh"
local Speed = Instance.new("IntValue", leaderstats)
Speed.Name = "Speed"
local PlayerData
local Success, ErrorMsg = pcall(function()
PlayerData = Data1:GetAsync(player.UserId)
end)
if Success then
if PlayerData then
Cash.Value = PlayerData[1]
Strengh.Value = PlayerData[2]
GripStrengh.Value = PlayerData[3]
Speed.Value = PlayerData[4]
end
else
warn(ErrorMsg)
end
end)
local function SaveData(player)
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
print("leaderstats")
end
local PlayerData = {
leaderstats.Cash.Value,
leaderstats.GripStrengh.Value,
leaderstats.Strengh.Value,
leaderstats.Speed.Value
}
local Success, ErrorMsg = pcall(function()
Data1:SetAsync(player.UserId, PlayerData)
end)
if Success then
print("Saved data")
print(PlayerData)
else
warn(ErrorMsg)
end
end
Players.PlayerRemoving:Connect(function(player)
SaveData(player)
end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
task.spawn(function()
SaveData(player)
end)
end
task.wait(5)
end)
I just tested it in a test game and it works. You are most likely changing the leaderstat values on the client, not the server which is why it prints 0.
Yes, you have to change them with a normal script or a module script. If you are manually changing the values in the explorer, then make sure you click this button first.
I know, that is why I said to use a normal script or a server script
That is why the script isn’t working. If you change the values on the client, it will not replicate to the server. You might want to fire a remote event from the client to the server.