Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

AI sight system erroring for no apparent reason?

Asked by 3 years ago

I've recently been working a game that needed a smarter AI, and the only one I could find has one function that always errors for me, even though I directly copied it after it errored the first time, it still errors though

this is a normal script in a model in the workspace

function CheckSight(target)
    local ray = Ray.new(MyRoot.Position, (target.Position - MyRoot.Position).Unit * 40)
    local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, (script.Parent))

    if hit then

        if hit:IsADescendentOf(target.Parent) and math.abs(hit.Position.Y - MyRoot.Position.Y) < 2 then
            print("visible")
            return true
        end
    end
    return false
end

it always says "unable to cast object to value", but it worked just fine for the guy who made the original script.

so if anyone has any idea why its erroring, please tell me, I'm trying to have the game out this year (bad idea, but it was my brother's idea not mine)

2 answers

Log in to vote
1
Answered by 3 years ago

On line 3 replace local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, (script.Parent)) with local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent})

This is necessary because the second argument should be a table containing all the objects to ignore.

Also, this method is deprecated so you should be using WorldRoot:Raycast() instead. You can read more about it here: https://developer.roblox.com/en-us/api-reference/function/WorldRoot/Raycast

Ad
Log in to vote
0
Answered by 3 years ago

thanks for the help, I never would have noticed how the brackets were the wrong ones :) although the ray system you said I should change doesn't seem to error at all, so I'm just going to leave it as it is for now, and if it starts to error, then I'll change it.

Answer this question