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

Why isn't part.touched:Connect(function()) firing?

Asked by 3 years ago

I am trying to make a butterfly that can fly around everywhere randomly. My issue is that it flies away from the baseplate meaning you can no longer see it. I've tried many ways trying to make it turn around when it hits the edge and none of them have been working, the butterfly either down right stops at the edge or it doesn't fly at all. My newest attempt is adding parts, then if the butterfly touches the part it turns around. Here is my current script and I'll try to explain it the best I can.

local Body = script.Parent.PrimaryPart -- The body of the Butterfly, so I can use its CFrame
local region = Region3.new(Vector3.new(-270, -8, -270), Vector3.new(270, 100, 270)) -- This is the region where the butterfly can fly in, when the butterfly leaves this region it just stops. It used to just fly off
local b1 = game.Workspace.ButterflyBoundaries.Baseplate -- Not referring to the actual baseplate but instead this directory takes you through a couple of folders to then find the Baseplate boundaries

local boundary = { -- All the boundaries that are in the Baseplate folder
    b1:WaitForChild("boundary1"), b1:WaitForChild("boundary2"),
    b1:WaitForChild("boundary3"), b1:WaitForChild("boundary4"),
    b1:WaitForChild("boundary5"), b1:WaitForChild("boundary6")
}

while true do
    wait()
    local partsinregion = workspace:FindPartsInRegion3(region, nil, math.huge) -- checks parts in region3
    for i, part in pairs(partsinregion) do
        if part.Parent:FindFirstChild("ConfirmedButterfly") then -- checks if the part is a butterfly
                script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.Angles(math.rad(math.random(-5,5)), math.rad(math.random(-5,5)), math.rad(math.random(-5,5)))) -- rotates the butterfly in a random direction
                wait()
            script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.new(1,0,0)) -- makes the butterfly fly in whatever direction it is looking            
            boundary[1].touched:Connect(function() -- from here onwards is where my script isn't working. When the butterfly touches the boundary it flies right through it
                script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.Angles(math.rad(90), math.rad(90), math.rad(90)))
            end)

-- Here onwards is repeated for what I did with boundary[1] but with each of the other boundaries
            boundary[2].touched:Connect(function()
                    script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.Angles(math.rad(90), math.rad(90), math.rad(90)))
            end)    
            boundary[3].touched:Connect(function()
                        script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.Angles(math.rad(90), math.rad(90), math.rad(90)))
            end)    
            boundary[4].touched:Connect(function()
                            script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.Angles(math.rad(90), math.rad(90), math.rad(90)))
            end)    
            boundary[5].touched:Connect(function()
                                script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.Angles(math.rad(90), math.rad(90), math.rad(90)))
            end)    
            boundary[6].touched:Connect(function()
                script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.Angles(math.rad(90), math.rad(90), math.rad(90)))
                end)    
            end
        end 
    end

I'm unsure if there is a quicker or easier way to do this, any help would be appreciated. I'm fairly new to scripting and this is my first full fledged script that I've written by myself.

1 answer

Log in to vote
1
Answered by 3 years ago

Lua is case sensitive. Use Touched instead of touched.

0
Yeh I agree you gotta make it exact ``Touched`` DuckyRobIox 280 — 3y
Ad

Answer this question