I made a script where you it should save the presents, but its working but giving a error “Unable to cast value to Object” sometimes:
local DS = game:GetService("DataStoreService"):GetDataStore("PresentsDataStoreFRFR5")
game.Players.PlayerAdded:Connect(function(plr)
wait()
local plrkey = "id_"..plr.userId
local savevalue = plr.PresentsStat.Presents
local savevalue2 = plr.PresentsStat.Present1
local savevalue3 = plr.PresentsStat.Present2
local savevalue4 = plr.PresentsStat.Present3
local savevalue5 = plr.PresentsStat.Present4
local savevalue6 = plr.PresentsStat.Present5
local savevalue7 = plr.PresentsStat.Present6
local savevalue8 = plr.PresentsStat.Present7
local savevalue9 = plr.PresentsStat.Present8
local GetSaved = DS:GetAsync(plrkey)
if GetSaved then
savevalue.Value = GetSaved[1]
savevalue2.Value = GetSaved[2]
savevalue3.Value = GetSaved[3]
savevalue4.Value = GetSaved[4]
savevalue5.Value = GetSaved[5]
savevalue6.Value = GetSaved[6]
savevalue7.Value = GetSaved[7]
savevalue8.Value = GetSaved[8]
savevalue9.Value = GetSaved[9]
else
local NumberForSaving = {savevalue.Value,savevalue2.Value,savevalue3.Value,savevalue4.Value,savevalue5.Value,savevalue6.Value,savevalue7.Value,savevalue8.Value,savevalue9.Value}
DS:GetAsync(plrkey,NumberForSaving)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
DS:SetAsync("id_"..plr.userId,{plr.PresentsStat.Presents.Value,plr.PresentsStat.Present1.Value,plr.PresentsStat.Present2.Value,plr.PresentsStat.Present3.Value,plr.PresentsStat.Present4.Value,plr.PresentsStat.Present5.Value,plr.PresentsStat.Present6.Value,plr.PresentsStat.Present7.Value,plr.PresentsStat.Present8.Value})
end)
How do i fix this error?