#whats wrong with my data currency saving code
6 messages · Page 1 of 1 (latest)
local DSS = game:GetService("DataStoreService")
local mainDS = DSS:GetDataStore("CashData")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage:FindFirstChild("Events")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Cash = Instance.new("IntValue", leaderstats)
Cash.Name = "Cash"
local data = mainDS:GetAsync(player.UserId)
if data then
if data[1] then
Cash.Value = data[1]
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
local Cash = leaderstats:WaitForChild("Cash")
mainDS:SetAsync(player.UserId, {Cash.Value})
end)
if data then
if data.Cash then
Cash.Value = data.Cash
end
end
local data = {['Cash'] = Cash.Value}
mainDS:SetAsync(player.UserId, data)
the first if statements go where you first load the data
And the last local data stuff goes where you were saving it after the cash variable
Typing on phone so correct spelling mistakes