Unable to cast to Array

Hello so i got this script:

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.

u need to put both of ur stats in an array

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)

Alright ill try the script you sent me.

It didn’t work. what it did is actually reset every value i had. i had 2k cash now im at 0. (same with the waves)

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)

it worked tysm brooooooooooooo!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.