I need help with data saving script

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

end

Players.PlayerRemoving:Connect(function(player)
SaveData(player)
end)

game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
SaveData(player)
end
end)

Make sure API services are enabled. Particularly “Studio Access to API services” in game>settings>security

ive did it and nothing happend

  1. Add a task.wait(5) in your :BindToClose function
  2. Test the script in game and see if it works. Some people have issues with saving in Studio.

Also, your code will just yield anyways in the BindToClose function unless you do this:

game:BindToClose(function()
    for _, player in pairs(game.Players:GetPlayers()) do
        task.spawn(function()
            SaveData(player)
        end)
    end
end)
1 Like

it still doesnt work any other solutions?

Is it giving you any errors? Also, add some print statements after the data is saved and when it loads.

1 Like

still doesnt work and i dont have any errors

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

1 Like

It isn’t an issue for me, however, many other people seem to have the issue which is why I bring it up in datastore related posts.

1 Like

so what i need to do to save player data

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)
1 Like

now you did to print player data but when i leave the game and open printed player data table it prints 0 everywhere

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.

1 Like

so what i need to do to change the values on server

1 Like

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.

image

1 Like

yea but i want to change the values on server with a script

i have one script that changes values but on client

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.

sorry if i sounded rude in this reply

1 Like

np but still it doesnt save the data like when i join a game the cash is like 400 and then when i rejoin it starts over from 0

Can you show me the script that changes the leaderstat value?

1 Like