Data saving script is not working

Ok so I had leaderstats already but, I was trying to get a data save too but when I changed script data save nor leaderstats were there. what’s wrong?

dss = game:GetService(“DataStoreService”)

local DataStoreToKeep = dss:GetDataStore(“TypeWhatYouWanthere”)

game.Players.PlayerAdded:Connect(function(player)
local is = Instance.new(“Folder”,player)
is.Name = “leaderstats”

local money = Instance.new(“IntValue”,is)
money.Name = “Money”
money.Value = 0

local data
local success, errormessage = pcall(function()
data = DataStoreToKeep:GetAsync(player.UserId…"~money")
end)
if success then
money.Value = data
else
warn(errormessage)
end
end)

game.Players.PlayerRemoving:Connect(function(player)

local success, errormessage = pcall(function()
DataStoreToKeep:GetAsync(player.UserId…"~money",player.leaderstats.Money.Value)
end)
if success then
else
warn(errormessage)
end
end)

2 Likes


here’s how it look.

1 Like

:GetAsync() only has 1 parameter (string).

still not working…

Try adding a :BindToClose() function at then end of your script:

game:BindToClose(function()
    for i, player in pairs(game.Players:GetPlayers()) do
        DataStoreToKeep:SetAsync(player.UserId.."~money", player.leaderstats.Money.Value)
    end
end)
2 Likes

can you show me exactly where to put it?

1 Like

You made a small typo :slight_smile: . In the Players.PlayerRemoving event function you want to set the data using :SetAsync() not :GetAsync(). No worries. I get typing mistakes all the time! Remember to post this as your solution if it works!

1 Like

If you look at the pictures, you can see that he already solved that mistake.

1 Like

If you look at my recent comment, that still didn’t work… is there anything else that’s wrong?

1 Like

Completely at the end, behind the PlayerRemoving event

1 Like

like this?

1 Like

No, put the whole code at the end of the script, not inside the removing function.

1 Like

:BindToClose Is a seperate function than PlayerRemoving. You would want to put the BindToClose part under the Players.PlayerRemoving.

I know you’re probably busy but can you just script it for me.

I’m on Mobile, so if it looks weird i’m sorry:

game.Players.PlayerRemoving:Connect(function(player)

local success, errormessage = pcall(function()
DataStoreToKeep:SetAsync(player.UserId.."~money",player.leaderstats.Money.Value)
end)
if success then
else
warn(errormessage)
end
end)


game:BindToClose(function()
    for i, player in pairs(game.Players:GetPlayers()) do
        DataStoreToKeep:SetAsync(player.UserId.."~money", player.leaderstats.Money.Value)
    end
end)

1 Like

That’s not what this website is for :slight_smile: I really suggest learning scripting Lua yourself and don’t rely on free model code you don’t understand

2 Likes

This isn’t a free model code… I watched a tutorial learning how to script, I’m not relying on anything… I’m just tired of working on a script that’s not working for 3 hours straight.

1 Like

Maybe you disabled the studio’s access to API services.

Try (in-studio):

  1. Find Game Settings at Home
  2. Go to Security
  3. Turn on the Enable Studio Access to API Services
  4. Click Save

Or (at your game page):

  1. Find the three dots button and click Configure this Experience
  2. Set Enable Studio Access to API Services into True
  3. Click Save
1 Like

This made it work, I’m so happy now, tysm

1 Like