Well you may have trouble saving data because of this first line here. You didn’t put an = between errormessage and pcall. That may be preventing the game from saving data. I would try that.
We talked it over in DMs and turns out it was not saving because in studio the code stopped running before the data saved. Solved it by giving it time to save with BindToClose
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")
game.Players.PlayerAdded:connect(function(plr)
local stats = Instance.new('Folder', plr)
stats.Name = 'leaderstats'
local values = Instance.new('Folder', plr)
values.Name = 'Values'
local Cash = Instance.new('IntValue', stats)
Cash.Name = 'Cash'
Cash.Value = 0
local Rebirth = Instance.new('IntValue', stats)
Rebirth.Name = 'Rebirths'
Rebirth.Value = 0
local Multiplier = Instance.new("IntValue", values)
Multiplier.Name = "Multiplier"
Multiplier.Value = 1
local clickCash = Instance.new('IntValue', values)
clickCash.Name = 'clickCash'
clickCash.Value = 1
local playerUserId = "Player_"..plr.UserId
local data
local success, errormessage = pcall(function()
data = DataStore:GetAsync(playerUserId)
end)
if success then
Cash.Value = data.Cash
Rebirth.Value = data.Rebirths
Multiplier.Value = data.Multiplier
clickCash.Value = data.clickCash
end
print("Data Loaded")
end)
function saveData(player)
local UserId = player.UserId
local data = {
Cash = player.leaderstats.Cash.Value;
Rebirths = player.leaderstats.Rebirths.Value;
Multiplier = player.Values.Multiplier.Value;
clickCash = player.Values.clickCash.Value
}
local success, errormessage = pcall(function()
DataStore:SetAsync(UserId, data)
end)
if not success then
warn(errormessage)
end
end
game.Players.PlayerRemoving:Connect(function(player)
saveData(player)
end)
game:BindToClose(function()
if RunService:IsStudio() then
return
end
local players = Players:GetPlayers()
for _, player in pairs(players) do
saveData()
end
end)