In the PlayerRemoving event, you have a typo
In the else block, instead of using dataStore:GetAsync , you should use dataStore:SetAsync to save the player’s data:
local dataStore = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(player)
wait()
local playerId = "id_"..player.UserId
local save1 = player.leaderstats.Cash
local GetSaved = dataStore:GetAsync(playerId)
if GetSaved then
save1.Value = GetSaved[1]
else
local NumberForSaving = {save1.Value}
dataStore:SetAsync(playerId, NumberForSaving) -- Fixed typo here
end
end)
game.Players.PlayerRemoving:Connect(function(player)
dataStore:SetAsync("id_"..player.userId, {player.leaderstats.Cash.Value})
end)
If this doesn’t solve the issue, make sure that the DataStore is enabled in the game’s configuration page, and that the data store key "SaveData" is spelled correctly.
There is quite a bit wrong with this script, but I don’t have much time to explain everything. I have to go in a few minutes, however, I do have a large post on DataStores, and maybe something in it can help.
It might be because you aren’t using pcalls (which you really should), or because the server closes before it saves. You should always add a game:BindToClose() when using DataStores. That’s just what I think could be the issue, though. Here is the big post:
I tried inserting this script and it still doesn’t save, do i need to also send the leaderstats script? Maybe there is a problem but it certain not be.
This is the code for the phone that gives you cash
local tool = script.Parent
local Player = tool.Parent.Parent
local Cash = Player.leaderstats.Cash
local Value = 2
local GamepassID = 146320044
script.Parent.Activated:Connect(function(player)
if not script.Parent.Ringtone.IsPlaying then
if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(Player.UserId, GamepassID) then
Cash.Value = Cash.Value + Value * 2
else
Cash.Value = Cash.Value + Value
end
script.Parent.Ringtone:Play()
else
--nothing lol
end
end)
and this is the leaderstats script.
local Players = game:GetService("Players")
local function leaderboardSetup(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Value = 0
Cash.Parent = leaderstats
end
Players.PlayerAdded:Connect(leaderboardSetup)
The script looks fine and should work correctly. However, there is one potential issue you may want to consider.
The code assumes that script.Parent refers to a Tool object. If the script is placed in a different object (e.g. a part), then script.Parent will refer to that object instead and the script may not work as intended. It’s a good practice to explicitly set the tool variable to the Tool object instead of relying on script.Parent .
Other than that, the script should work as intended
I can give you code by person called @TheDevKing I always use it to save data
local DataStoreService = game:GetService("DataStoreService")
local PlayersService = game:GetService("Players")
local DataStore = DataStoreService:GetDataStore("DataStore")
PlayersService.PlayerAdded:Connect(function(Player)
Player:WaitForChild("leaderstats")
local leaderstats = Player:FindFirstChild("leaderstats")
leaderstats:WaitForChild("Cash")
local Cash = leaderstats:FindFirstChild("Cash")
local PlayerUserId = "Player_"..Player.UserId
--Load Data--
local Data
local success, erromessage = pcall(function()
Data = DataStore:GetAsync(PlayerUserId)
end)
if success == true then
if Data then
Cash.Value = Data.Cash
end
end
end)
PlayersService.PlayerRemoving:Connect(function(Player)
local PlayerUserId = "Player_"..Player.UserId
local Data = {
Cash = Player.leaderstats.Cash.Value
}
local success, errormessage = pcall(function()
DataStore:SetAsync(PlayerUserId, Data)
end)
if success then
print("Data successfully saved!")
else
print("There was an error!")
warn(errormessage)
end
end)
Also check if you have in settings in security Enable Studio Access to API Services Enabled