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)
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)