Hello, I followed @GEILER123456’s tutorial on how to make a datastore, but the error keep printing out “There was an error with saving a player’s data Unable to cast to array”. What does this error mean, and how could it be fixed?
Here is the script:
local dataStoreService = game:GetService("DataStoreService")
local players = game:GetService("Players")
local dataStore = dataStoreService:GetDataStore("Name")
local function leaderboardSetUp(player)
local userId = player.UserId
local key = "Player_" .. userId
local data = dataStore:GetAsync(key)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local wins = Instance.new("IntValue")
wins.Name = "Wins"
wins.Value = data or 0
wins.Parent = leaderstats
local money = Instance.new("IntValue")
money.Name = "Money"
money.Value = data or 0
money.Parent = leaderstats
print("Leaderstats loaded.")
end
local function save(player)
local userId = player.UserId
local key = "Player_" .. userId
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local winsValue = leaderstats.Wins.Value
local moneyValue = leaderstats.Money.Value
local success, ret = pcall(dataStore.GetAsync, dataStore, key, winsValue, moneyValue)
if success then
print("Data has been saved successfully!")
else
print("There was an error with saving a player's data" .. ret)
end
end
end
local function onShutDown()
wait(1)
end
game:BindToClose(onShutDown)
players.PlayerAdded:Connect(leaderboardSetUp)
players.PlayerRemoving:Connect(save)
while true do
wait(60)
for _,player in ipairs(players:GetPlayers()) do
coroutine.wrap(save)(player)
end
end
Thank you.