✓ Accepted answer
You don't want to redefine the function; Technically a solution like that could be made to work, but it's a bad one because it's very confusing. (For the curious, how it would look)
Instead, just say what you mean, but use an else clause:
function onClicked(playerWhoClicked)
if not r then
r = true
else
r = false
end
end
This could be shortened to simply
function onClicked(playerWhoClicked)
r = not r
end
The remainder of the code will only happen once, at the beginning of the script.
You should use a while loop to make it repeatedly act:
script.Parent.ClickDetector.MouseClick:connect(onClicked)
while wait() do
if r then
script.Parent.Parent.door.Transparency = 1
script.Parent.Parent.door.CanCollide = false
else
script.Parent.Parent.door.Transparency = 0
script.Parent.Parent.door.CanCollide = true
end
end
Of course, since this the loop only changes anything when r changes, we might as well put it in the function (the only place where r changes), meaning the entire script would just be
r = false
function onClicked(playerWhoClicked)
if not r then
r = true
script.Parent.Parent.door.Transparency = 1
script.Parent.Parent.door.CanCollide = false
else
r = false
script.Parent.Parent.door.Transparency = 0
script.Parent.Parent.door.CanCollide = true
end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
Edit: paste didn't include the connection line (don't just blindly copy and paste, actually look at it and analyze -maybe go through the changes yourself to learn how in the future)
1 comment
Works!