Here's my code. There is a ClickDetector.
01 | r = false |
02 |
03 | function onClicked(playerWhoClicked) |
04 | r = true |
05 | if r then |
06 | function onClicked(playerWhoClicked) |
07 | r = false |
08 | end |
09 | end |
10 | end |
11 |
12 | if r then |
13 | script.Parent.Parent.door.Transparency = 1 |
14 | script.Parent.Parent.door.CanCollide = false |
15 | else |
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:
1 | function onClicked(playerWhoClicked) |
2 | if not r then -- (if r is false) |
3 | r = true |
4 | else |
5 | r = false |
6 | end |
7 | end |
This could be shortened to simply
1 | function onClicked(playerWhoClicked) |
2 | r = not r -- "Flips" r. false --> true, true --> false |
3 | 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:
01 | script.Parent.ClickDetector.MouseClick:connect(onClicked) |
02 | -- Connection line must happen *before* start of loop |
03 | while wait() do |
04 | -- Every 1/30 seconds... |
05 | if r then |
06 | script.Parent.Parent.door.Transparency = 1 |
07 | script.Parent.Parent.door.CanCollide = false |
08 | else |
09 | script.Parent.Parent.door.Transparency = 0 |
10 | script.Parent.Parent.door.CanCollide = true |
11 | end |
12 | 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
01 | r = false |
02 |
03 | function onClicked(playerWhoClicked) |
04 | if not r then -- (if r is false) |
05 | r = true |
06 | script.Parent.Parent.door.Transparency = 1 |
07 | script.Parent.Parent.door.CanCollide = false |
08 | else |
09 | r = false |
10 | script.Parent.Parent.door.Transparency = 0 |
11 | script.Parent.Parent.door.CanCollide = true |
12 | end |
13 | end |
14 |
15 | 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)
Not a fan of that script's format. Possibly because the if
statement will only run once.
Have it run inside of a function.
01 | r = false |
02 |
03 | function onClicked() |
04 | if r then |
05 | r = false |
06 | script.Parent.Parent.door.Transparency = 1 |
07 | script.Parent.Parent.door.CanCollide = false |
08 | else |
09 | r = true |
10 | script.Parent.Parent.door.Transparency = 0 |
11 | script.Parent.Parent.door.CanCollide = true |
12 | end |
13 | end |
14 |
15 | script.Parent.ClickDetector.MouseClick:connect(onClicked) |
Does it have a click detector?
I honestly don't know, I'm a very novice scripter, but try this
01 | r = false |
02 |
03 | function onClicked(playerWhoClicked) |
04 | r = true |
05 | if r = true then |
06 | end |
07 | end |
08 |
09 | if r = true then |
10 | script.Parent.Parent.door.Transparency = 1 |
11 | script.Parent.Parent.door.CanCollide = false |
12 | else |
13 | script.Parent.Parent.door.Transparency = 1 |
14 | script.Parent.Parent.door.CanCollide = true |
15 | end |
16 |
17 |
18 | script.Parent.ClickDetector.MouseClick:connect(onClicked) |
Again, probably wrong.