Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does this door move even though the click detector isn't clicked?

I'm trying to make a door that will go up when the button is clicked, and when it's clicked again the door will go down but the door goes up at the start of the game for no reason, the output says nothing.

local tweenService = game:GetService("TweenService")
local seconds = 10.104
local isOn = false

function on()
 isOn = true
script.Parent.Sound:Play()
tweenService:Create(script.Parent, TweenInfo.new(seconds, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {CFrame = CFrame.new(104.855, 4.757, -148.31)}):Play()
end

function off()
script.Parent.Sound:Play()
tweenService:Create(script.Parent, TweenInfo.new(seconds, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {CFrame = CFrame.new(104.87, -4.885, -148.2)}):Play()
end

function onClicked()

 if isOn == true then off() else on() end

end

workspace.Button.ClickDetector.MouseClick:connect(onClicked)

on()

Thanks if you can help.

2 answers

✓ Accepted answer

The problem here seems to be the bottom of your script, where you are calling on().

It appears that on() is used to open the door.

Side note: Don't use connect, it's deprecated. Use Connect instead (note the capital 'C').

1 comment

0

Omg I always forget about is Connect

This is due to line 24.

First, You’re connecting your onClicked function to the MouseClick event correctly, but on that last line, you’re calling the on() function for no reason. This calls it before the event fires. Remove the on().

And also, switch to Connect, as ROBLOX will remove connect in the future.

And on your onClicked function, you crammed everything into one line. Bad practice.

function onClicked(player)
    if isOn then
        on()
    else
       off()
    end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.