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

When I click a part with a click detector, the function doesn't work. Why?

Asked by 6 years ago

So I'm trying to make it so when you click a part, the transparency for something goes to one, but it doesn't work. Here is the script:

01local door = game.Workspace.RDoor
02local clickDetector = Instance.new("ClickDetector")
03clickDetector.Parent = script.Parent
04local lighton = 0
05 
06function light()
07    print("light")
08    if lighton == 0 then
09        door.Transparency = 1
10        lighton = 1
11    end
12    if lighton == 1 then
13        door.Transparency = 0
14        lighton = 0
15    end
16end
17 
18clickDetector.MouseClick:Connect(light)

2 answers

Log in to vote
0
Answered by 6 years ago

You're very close to the answer with this script, there's just a small error. Basically, rather than assigning >lighton to an integer, you want to create a Boolean (a value that can only be true or false). If we change this, your script should work:

01local door = game.Workspace.RDoor
02local clickDetector = Instance.new("ClickDetector")
03clickDetector.Parent = script.Parent
04local lighton = false
05 
06function light()
07    print("light")
08    if lighton == false then
09        door.Transparency = 1
10        lighton = true
11    end
12    if lighton == true then
13        door.Transparency = 0
14        lighton = false
15    end
16end
17 
18clickDetector.MouseClick:Connect(light)

Ok, our script is working. But believe it or not, we can simplify this, which you should always try to do, as it saves you work.

01local door = game.Workspace.RDoor
02local clickDetector = Instance.new("ClickDetector")
03clickDetector.Parent = script.Parent
04local lighton = false
05 
06function light()
07    print("light")
08    if lighton then --Fig.A
09        door.Transparency = 0
10        lighton = false
11    else --Fig.B
12        door.Transparency = 1
13        lighton = true
14    end
15end
16 
17clickDetector.MouseClick:Connect(light)

As you can see, there is less to type out now. What i've done is simple, for starters (we'll look at Fig.A) since our >lighton value is now a boolean, it's only ever going to be true or false right? And an if statement is going to execute the code within if the condition >lighton are true, if you look at Fig.B now, you'll see that i've changed your code a bit.

Basically, if >lighton is not true, then do whats within the else statement. Now we're talking. If you really wanted to you could shorten this code even further, but that isn't necessary so i'll leave that for another time.

Useful Links:

Conditional Statements (I recommend looking at Else and ElseIf statements.)

Booleans

Ad
Log in to vote
0
Answered by
aazkao 787 Moderation Voter
6 years ago
Edited 6 years ago

Scripts run from the top down, what is happening is when it changes the transparency to 1 and lighton to 1, the next statementif lighton == 1 becomes true and it changes the transparenct back to 0, which is why it looks like its not working but it actually is, the code was just written wrong for what you need

You want to nest the if statement to ensure that the second condition wont run after changing your lighton value.

01local door = game.Workspace.RDoor
02local clickDetector = Instance.new("ClickDetector")
03clickDetector.Parent = script.Parent
04local lighton = 0
05 
06function light()
07    print("light")
08    if lighton == 0 then
09        door.Transparency = 1
10        lighton = 1
11 
12   else  if lighton == 1 then
13        door.Transparency = 0
14        lighton = 0
15    end
16    end
17end
18 
19clickDetector.MouseClick:Connect(light)

Here is a shorter way to do it with just 1 if statement

01local door = game.Workspace.RDoor
02local clickDetector = Instance.new("ClickDetector")
03clickDetector.Parent = script.Parent
04 
05 
06function light()
07 
08    if door.Transparency == 0 then
09       door.Transparency = 1
10    else
11       door.Transparency = 0
12    end  
13end
14 
15clickDetector.MouseClick:Connect(light)

or just 1 line of code using quick maths

1local door = game.Workspace.RDoor
2local clickDetector = Instance.new("ClickDetector")
3clickDetector.Parent = script.Parent
4 
5function light()
6door.Transparency = (door.Transparency -1)*-1
7end
8 
9clickDetector.MouseClick:Connect(light)
0
This answer is better :3 turtle2004 167 — 6y
0
Although, i'm pretty sure you can just do, door.Transparency = -(door.Transparency -1) turtle2004 167 — 6y
0
well its the same thing but i guess we are going for minimalist code here lol aazkao 787 — 6y
0
exactly, programmers are lazy ;3 turtle2004 167 — 6y

Answer this question