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

How can I fix this part finding function?

Asked by 8 years ago

I'm trying to make a function that returns false only if ANY part in workspace is within a certain range of a part called CSprout. My attempt is below and I'm not sure what I need to do to make it work. This isn't the whole script, just the function I'm trying to fix. If I need to clarify anything let me know. Thanks!

local seed = script.Parent
local position = seed.Position
local RANGE = 5

function spaceCheck(CSprout)
    local OTHER = game.Workspace:GetChildren()

    for index, child in pairs(OTHER) do
    print(OTHER.Name)
    if OTHER:IsA("Part") and (CSprout.Position - OTHER.Position).magnitude < RANGE then
        return false 
            else return true 
    end
end
end

1 answer

Log in to vote
2
Answered by 8 years ago

You're checking if OTHER is a Part, while OTHER is actually the contents of Workspace. You need to check for child instead, such that:

function spaceCheck(CSprout)
    local OTHER = game.Workspace:GetChildren()
    for index, child in pairs(OTHER) do
    print(child.Name)
    if child:IsA("BasePart") and (CSprout.Position - child.Position).magnitude < RANGE then
        return false
            else return true
    end
end
end

I also changed Part to BasePart to work with wedges and other parts under the BasePart category.

1
Thank you! Gwolflover 80 — 8y
Ad

Answer this question