I keep getting a error on line 58 saying “Unable to cast value to Object”
anyway to fix this?
data storage is not my strong point ;(
local players = game.Players
local dsService = game:GetService("DataStoreService")
local datastore = dsService:GetDataStore("InstanceSave")
local prefix = "Player: "
local location = workspace.Folder
local storage = game.ReplicatedStorage.Placables
function save(player:Player)
local key = prefix .. tostring(player.UserId)
local data = {}
for index, model:Model in ipairs(location:GetChildren()) do
table.insert(data,{
model.Name,
model:GetPivot()
})
end
local success, err
repeat
success, err = pcall(function()
datastore:UpdateAsync(key, data)
end)
task.wait()
until success
if not success then
warn("!Error! == "..tostring(err))
end
end
function load(player:Player)
local key = prefix .. tostring(player.UserId)
local data = {}
local success, err
repeat
success, err = pcall(function()
datastore:GetAsync(key, data)
end)
task.wait()
until success or not players:FindFirstChild(player.Name)
if not data then return end
if success then
for index, model in ipairs(data) do
local new = storage:FindFirstChild(model[1]):Clone()
new.Parent = location
new:PivotTo(model[2])
end
else
warn("!Error! == "..tostring(err))
end
end
players.PlayerAdded:Connect(load)
players.PlayerRemoving:Connect(save)
game:BindToClose(function()
for index, player in ipairs(players:GetPlayers()) do
save(player)
end
end)
This is weird, line 58 looks… perfectly fine. Under normal circumstances, tostring() should always return something rather than throwing an error, so it doesn’t make sense that you would get an “Unable to cast value to Object” even if err is equal to nil.
Are you sure the error is from line 58? Maybe you meant some other line?
If not, then try printing err and see what happens, it should (hopefully) throw an error.
So, I added a couple lines to see what was the error and success doing and I found out that the error can’t cast the value to the object, but the success is just false.
local players = game.Players
local dsService = game:GetService("DataStoreService")
local datastore = dsService:GetDataStore("InstanceSave")
local prefix = "Player: "
local location = workspace.Folder
local storage = game.ReplicatedStorage.Placables
function save(player:Player)
local key = prefix .. tostring(player.UserId)
local data = {}
for index, model:Model in ipairs(location:GetChildren()) do
table.insert(data,{
model.Name,
model:GetPivot()
})
end
local success, err
repeat
success, err = pcall(function()
datastore:UpdateAsync(key, data)
end)
task.wait()
until success
-- After that, it doesn't print anymore
print(err)
print(success)
if not success then
warn("!Error! == "..tostring(err))
else
return
end
end
function load(player:Player)
local key = prefix .. tostring(player.UserId)
local data = {}
local success, err
repeat
success, err = pcall(function()
datastore:GetAsync(key, data)
end)
task.wait()
until success or not players:FindFirstChild(player.Name)
if not data then return end
print(err) -- can't cast the object
print(success) -- is false
if success then
for index, model in ipairs(data) do
local new = storage:FindFirstChild(model[1]):Clone()
new.Parent = location
new:PivotTo(model[2])
end
else
warn("!Error! == "..tostring(err)) -- prints that it can't cast the value to the object
end
end
players.PlayerAdded:Connect(load)
players.PlayerRemoving:Connect(save)
game:BindToClose(function()
for index, player in ipairs(players:GetPlayers()) do
save(player)
end
end)
And I tried some more, and I eventually got it (hopefully):
local players = game.Players
local dsService = game:GetService("DataStoreService")
local datastore = dsService:GetDataStore("InstanceSave")
local prefix = "Player: "
local location = workspace.Folder
local storage = game.ReplicatedStorage.Placables
function save(player:Player)
local key = prefix .. tostring(player.UserId)
local data = {}
for index, model:Model in ipairs(location:GetChildren()) do
table.insert(data,{
model.Name,
model:GetPivot()
})
end
local success, err = pcall(function()
if dsService == nil then
warn(dsService.Name.." is nil")
end
end)
print(err) -- prints nil
print(success) -- is true
task.wait()
if not success then
warn("!Error! == "..tostring(err)) -- doesn't print because success is true
else
return
end
end
function load(player:Player)
local key = prefix .. tostring(player.UserId)
local data = {}
local success, err = pcall(function()
if dsService == nil then
warn(dsService.Name.." is nil")
end
end)
if not data then return end
print(err) -- prints nil
print(success) -- is true
if success then
for index, model in ipairs(data) do
local new = storage:FindFirstChild(model[1]):Clone()
new.Parent = location
new:PivotTo(model[2])
end
else
warn("!Error! == "..tostring(err)) -- doesn't print because success is true
end
end
players.PlayerAdded:Connect(load)
players.PlayerRemoving:Connect(save)
game:BindToClose(function()
for index, player in ipairs(players:GetPlayers()) do
save(player)
end
end)
This script (above the line) usually fixes the problem of the Datastore not working.
And it’s mostly that (don’t copy please):
--Imagine if there was 20 lines before
local success, error = pcall(function()
if Datastore == nil then --> What we're talking about is the datastore(service) itself
warn(Datastore.Name.."is nil")
end
end)