local stat = "Cash" --Change to your stat name
local startamount = 0 --Change to how much points the player will start out with
local boo
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 Data1 = Instance.new("Folder",player)
Data1.Name = "Bools"
local zone1 = Instance.new("BoolValue", Data1) -- if you have to use bool. but you will probably need new names for each zone. Zone1, zone2 etc, bool1,bool2 doesn't really explain what it's for
zone1.Name = "zone1"
zone1.Value = false -- default to false
local dataToSave = {Data1.zone1.Value}
game.Players.PlayerRemoving:connect(function(player)
ds:SetAsync(player.UserId, player.leaderstats.Cash.Value, player.Bools.zone1) --Change "Points" to the name of your leaderstat.
end)
end)
alright so basically everytime i leave i get Unable to cast to array in the output, this line is the problem:
The third parameter in ds:SetAsync is supposed to be an array of UserIds associated with the data. Correct me if I’m wrong, but it looks like you’re trying to pass an instance (BoolValue) instead.
If you want to save both the player’s cash and the bool value, you need to put them both into an array and pass that array as the second parameter. Additionally, you need to get the actual value of the BoolValue by writing player.Bools.zone1.Value, as instances can not be saved to DataStores.
local stat = "Cash" --Change to your stat name
local startamount = 0 --Change to how much points the player will start out with
local boo
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 Data1 = Instance.new("Folder",player)
Data1.Name = "Bools"
local zone1 = Instance.new("BoolValue", Data1) -- if you have to use bool. but you will probably need new names for each zone. Zone1, zone2 etc, bool1,bool2 doesn't really explain what it's for
zone1.Name = "zone1"
zone1.Value = false -- default to false
local dataToSave = {Data1.zone1.Value}
game.Players.PlayerRemoving:connect(function(player)
ds:SetAsync(player.UserId, {player.leaderstats.Cash.Value, player.Bools.zone1.Value}, {player.UserId}) --Change "Points" to the name of your leaderstat.
end)
end)
Now that you’re saving an array into the DataStore, you’ll want to write ds:GetAsync(player.UserId)[1] or startamount, as that is where their cash is now stored.
By putting this into the Players.PlayerAdded connection, every player that joins will connect the function to when any player leaves. E.g. If 3 players join, when one player leaves, that event will try save their data 3 times. This won’t be a problem right now (I assume you’re testing this alone, so it will only connect once), but in the future it will be. You can fix it either by moving it outside of the loop, or by changing it to player.AncestryChanged:Once(function().
Alright this worked but one more thing, im trying to make a pet simulator x zone type thingy, in which if u buy a zone u dont have to buy it again after rejoining, i tried doing it with the booleans but it doesnt work, could ya help me?
Basically i got a script inside a proximityprompt:
local plr = game:GetService("Players").PlayerAdded:Connect(function(plr)
local leadestats = plr:FindFirstChild("leaderstats")
local goldStats = leadestats and leadestats:FindFirstChild("Cash")
script.Parent.TriggerEnded:Connect(function()
if goldStats then
goldStats.Value -= 300
script.Parent.Parent:Destroy()
plr.Bools.zone1.Value = true
else
print("you cant buy this lol")
end
end)
end)
and it just takes away 300 cash and sets ur boonlean to be true, (and that datastore script should save it which it does i think)
and heres a different script that deletes the thingy if the boonlean value is true:
local plr = game.Players.LocalPlayer
if plr.Bools.zone1.Value == true then
script.Parent:Destroy()
end