local stat = "Cash" --Change to your stat name
local startamount = 255 --Change to how much points the player will start out with
local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("LeaderStatSave")
--Don't worry about the rest of the code, except for line 25.
game.Players.PlayerAdded:connect(function(player)
local leader = Instance.new("Folder",player)
leader.Name = "leaderstats"
local Cash = Instance.new("IntValue",leader)
Cash.Name = "Cash"
Cash.Value = ds:GetAsync(player.UserId) or startamount
local Waves = Instance.new("IntValue",leader)
Waves.Name = "Waves"
Waves.Value = ds:GetAsync(0)
end)
game.Players.PlayerRemoving:connect(function(player)
ds:SetAsync(player.UserId, player.leaderstats.Cash.Value, player.leaderstats.Waves.Value) --Change "Points" to the name of your leaderstat.
print("saved")
end)
and for some reason when i leave i get “Unable to cast to Array” how do i fix this?
(also the output says that this line is the problem)
ds:SetAsync(player.UserId, player.leaderstats.Cash.Value, player.leaderstats.Waves.Value) --Change "Points" to the name of your leaderstat.
game.Players.PlayerRemoving:connect(function(player)
ds:SetAsync(player.UserId, {player.leaderstats.Cash.Value, player.leaderstats.Waves.Value}) --Change "Points" to the name of your leaderstat.
print("saved")
end)
It works, you just have to change how you load data.
local stat = "Cash" --Change to your stat name
local startamount = 255 --Change to how much points the player will start out with
local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("LeaderStatSave")
--Don't worry about the rest of the code, except for line 25.
game.Players.PlayerAdded:connect(function(player)
local leader = Instance.new("Folder",player)
leader.Name = "leaderstats"
local Cash = Instance.new("IntValue",leader)
Cash.Name = "Cash"
Cash.Value = startamount
local Waves = Instance.new("IntValue",leader)
Waves.Name = "Waves"
Waves.Value = 0
local data = {}
local success, err = pcall(function()
data = ds:GetAsync(player.UserId)
end)
if success then
Cash.Value = data[1]
Waves.Value = data[2]
else
warn(err)
end
end)
game.Players.PlayerRemoving:connect(function(player)
local success, err = pcall(function()
ds:SetAsync({player.UserId, player.leaderstats.Cash.Value, player.leaderstats.Waves.Value})
end)
if success then print("Saved") else warn(err) end
end)