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

How to fix my script, when I insert a new ClickDetector, it doesn't work?

Asked by 4 years ago

Can someone help me? I'm trying to make an open and close part and when you click it, it disappears and the opened part appears. It opens and closes but it shows the click detector when it disappears, so it destroys itself when you click it in the script, and there is another script in the opened part that puts another click detector in the closed part, but the script doesn't recognize the click detector and does nothing.

The first part script:


function onClick()
    if script.Parent.Parent.patrock2.Transparency == 1 then
        script.Parent.Parent.patrock2.Transparency = 0
        script.Parent.Parent.windmill2.Transparency = 0
        script.Parent.Transparency = 1
        script.Parent.Parent.windmill1.Transparency = 1
        script.Parent.ClickDetector:Destroy()
        Instance.new("ClickDetector",script.Parent.Parent.patrock2)
    end
end
script.Parent.ClickDetector.MouseClick:connect(onClick)

--The second script below

function onClicked()
if script.Parent.Parent.patrock1.Transparency == 1 then
    script.Parent.Transparency = 1
    script.Parent.Parent.windmill2.Transparency = 1
    script.Parent.Parent.patrock1.Transparency = 0
    script.Parent.Parent.windmill1.Transparency = 0
    script.Parent.ClickDetector:Destroy()
    Instance.new("ClickDetector", script.Parent.Parent.patrock1)
end
end
script.Parent.ClickDetector.MouseClick:Connect(onClicked)
0
I cannot understand your problem. Can you make it clearer please? Y_VRN 246 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

When you instance a new ClickDetector, you never assign It to a Touched event. Try this:

function onClick()
    if script.Parent.Parent.patrock2.Transparency == 1 then
            script.Parent.Parent.patrock2.Transparency = 0
            script.Parent.Parent.windmill2.Transparency = 0
            script.Parent.Transparency = 1
            script.Parent.Parent.windmill1.Transparency = 1
            script.Parent.ClickDetector:Destroy()
            local cd1 = Instance.new("ClickDetector",script.Parent.Parent.patrock2)
        cd1.MouseClick:Connect(onClicked)
    end
end
script.Parent.ClickDetector.MouseClick:connect(onClick)

--The second script below

function onClicked()
    if script.Parent.Parent.patrock1.Transparency == 1 then
        script.Parent.Transparency = 1
        script.Parent.Parent.windmill2.Transparency = 1
        script.Parent.Parent.patrock1.Transparency = 0
        script.Parent.Parent.windmill1.Transparency = 0
        script.Parent.ClickDetector:Destroy()
        local cd2 = Instance.new("ClickDetector", script.Parent.Parent.patrock1)
        cd2.MouseClick:connect(onClick)
    end
end
script.Parent.ClickDetector.MouseClick:Connect(onClicked)
Ad

Answer this question