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

How to turn a findFirstChild into a variable?

Asked by 5 years ago

Hello, I have a function, and I want to find the destination of the model that passes through it. I have done it so it finds the part Destination, I just dont know what would go at the --something here comment. I am trying to get the value of Destination and then calling the appropriate. function.

local function onTouch(part)
    if part.Parent:findFirstChild("Destination") then
--Something here
        if Destination.Value == "York" then
            InL()
        end
    end     
end

script.parent.Collider.Touched:connect(onTouch)

If anyone could help that would be great!

Thanks.

1 answer

Log in to vote
0
Answered by 5 years ago

Please check the wiki on how to create and use Variables and also please use FindFirstChild.

You can create a local variable in the function to hold the return value of the function call FindFirstChild like this.

-- example of creating a function and assigning the value to the variable
local onTouch2 = function()

end

-- this is similar to the code above see https://i.imgur.com/rkGibSm.png
local function onTouch(part)
    local Destination = part.Parent:FindFirstChild("Destination")
    if Destination then
        if Destination.Value == "York" then
            InL()
        end
    end
end

script.parent.Collider.Touched:Connect(onTouch) -- pls use :Connect

Hope this helps.

Ad

Answer this question