Unable to cast to Array when using :SetAsync()

I am creating a saving system for the integer Money, It gives me the error “Unable to cast to Array” and I have no clue how to solve it.

Here’s my code:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local LeaderstatV = DataStoreService:GetDataStore("LV")


Players.PlayerRemoving:Connect(function(player)
	local Money = LeaderstatV:GetAsync(player.UserId,"-Money")
	
	print(Money)
	print(player.leaderstats.Money.Value)
	if Money == nil then
		LeaderstatV:SetAsync(player.UserId,"-Money",player.leaderstats.Money.Value)
	else 
		LeaderstatV:UpdateAsync(player.UserId,"-Money",player.leaderstats.Money.Value)
	end	
end)
1 Like

I was able to fix it by looking at my old code and doing this

LeaderstatV:SetAsync(player.UserId.."-Money",tostring(player.leaderstats.Money.Value))

instead of

LeaderstatV:SetAsync(player.UserId "-Money",tostring(player.leaderstats.Money.Value))
1 Like

Try changing this one line to this

local Money
local s,e = pcall(function()
    money = LeaderstatV:GetAsync(player.UserId) or 0
end)
if e then warn(e) end

And the other lines after if Money == nil then to this

local s,e = pcall(function()
    LeaderstatV:SetAsync(player.UserId,player.leaderstats.Money.Value)
end)
if e then warn(e) end

You need to add a pcall as well, since data store goes down often. If you don’t, the function will break with an error, if it goes down.

1 Like

I will look into pcalls thank you

Pcalls - When and how to use them try reading this. You also can drop the "-money" in all of the datastore related stuff.

I dropped the …"-Money" and I get the error “Unable to cast value to function”

Code:

LeaderstatV:UpdateAsync(player.UserId,tostring(player.leaderstats.Money.Value))

Why are you using tostring() for the integer/float? Also, you need to drop the “-Money” everywhere in your script for it to work.

I used it to try and solve the Unable to cast to Array error.

Saving Script:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local LeaderstatV = DataStoreService:GetDataStore("LV")


Players.PlayerRemoving:Connect(function(player)
	local Money
	local s,e = pcall(function()
		Money = LeaderstatV:GetAsync(player.UserId) or 0
	end)
	if e then warn(e) end
	
	if Money == nil then
		local s,e = pcall(function()
			LeaderstatV:SetAsync(player.UserId,player.leaderstats.Money.Value)
		end)
		if e then warn(e) end
	else 
		LeaderstatV:UpdateAsync(player.UserId,tostring(player.leaderstats.Money.Value))
	end	
end)

Loading Script:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local LeaderstatV = DataStoreService:GetDataStore("LV")

Players.PlayerAdded:Connect(function(player)
	local Money
	local s,e = pcall(function()
		Money = LeaderstatV:GetAsync(player.UserId) or 0
	end)
	if e then warn(e) end
	
	if Money ~= nil then
		player.leaderstats.Money.Value = tonumber(Money) 
	end
end)

You can save any type of value in a datastore.

Do you have an idea for the “Unable to cast value to function” error

Which line is it at? Can you just send me the line’s code?

Line of error:

	LeaderstatV:UpdateAsync(player.UserId,tostring(player.leaderstats.Money.Value))

I thought the rest of the script would be good so you know what info its getting

Use :SetAsync() instead [FILLER TEXT]

I should use :SetAsync() insted of :UpdateAsync() ?

1 Like

Yes, because they are not the same. And for what you’re doing, you want to use :SetAsync()

Thanks for helping me, It all works now. Have a good day!

1 Like