I was working on a game for about a week now, and now I was supposed to make a build system for it. I made a grid-based place system, which has no problems. Now, I’m trying to implement the delete block system, where I am running into an error: “Unable to cast value to Object”. I tried looking up the DevForum for this, and I got this topic. I tried using the :FindPartOnRayWithIgnoreList() method, but it’s still throwing up the same error.
My code:
for i,v in pairs(workspace:GetChildren()) do
local function checkForModels()
for j,x in pairs(v:GetChildren()) do
table.insert(ignoreList, x.Name)
if x:IsA("Model") or v:IsA("Folder") then
checkForModels()
end
end
end
if v.Name ~= player.Name.."Model" then
table.insert(ignoreList, v.Name)
if v:IsA("Model") or v:IsA("Folder") then
checkForModels()
end
end
end
unitRay = mouse.UnitRay
local ray = Ray.new(unitRay.Origin, unitRay.Direction * maxPlaceDistance)
local part, pos, normal = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList, false, true)
Above mentioned is just a snippet. Awaiting for response!
It appears your ignoreList contains strings. Strings (values) cannot be casted to objects (instances, as expected in your ignoreList).
Furthermore, checkForModels is called recursively without any parameters or exit condition. Once the recursive call is reached, it will end up in an infinite recursion and a stack overflow.
You should try adding the instance which’s children you want to check as a parameter and initially call it with v.
for i,v in pairs(workspace:GetChildren()) do
if v.Name ~= player.Name.."Model" then
table.insert(ignoreList, v)
if v:IsA("Model") or v:IsA("Folder") then
local function checkForModels()
for j,x in pairs(v:GetChildren()) do
table.insert(ignoreList, x)
if x:IsA("Model") or v:IsA("Folder") then
checkForModels()
end
end
end
checkForModels()
end
end
end
Now, it is not throwing any errors, and not working either. I’m not sure why.
Edit: I’m making the part in LocalScript (however I’ll change it soon). Is this the problem? Even the ray is created in the LocalScript.
Edit 2: Nevermind, silly mistake. Got it working. Marking as solved. Thanks for help!