I was trying to make a DataStore script for a simulator, but then I got an error in the output that said “Unable to cast value to Object”
I was using a tutorial, but I kept double checking, and my code was the same as theirs.
Here’s my script:
local DS = game:GetService("DataStoreService"):GetDataStore("Store-1")
game.Players.PlayerAdded:Connect(function(plr)
wait()
local plrkey = "id_"..plr.UserId
local savevalue1 = plr.leaderstats.Points
local GetSaved = DS:GetAsync(plrkey)
if GetSaved then
savevalue1.Value = GetSaved[1]
else
local ValuesToSave = {savevalue1.Value}
DS:GetAsync(plrkey, ValuesToSave)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local saveItems = {plr.leaderstats.Points.Value}
DS:SetAsync("id_"..plr.UserId, saveItems)
end)
game:BindToClose(function()
for i, plr in pairs(game.Players:GetChildren()) do
plr:Kick()
end
end)
Also, the error was on line 15, which was
DS:GetAsync(plrkey, ValuesToSave)