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:

local door = game.Workspace.RDoor
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = script.Parent 
local lighton = 0

function light()
    print("light")
    if lighton == 0 then
        door.Transparency = 1
        lighton = 1
    end
    if lighton == 1 then
        door.Transparency = 0
        lighton = 0
    end
end

clickDetector.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:

local door = game.Workspace.RDoor
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = script.Parent 
local lighton = false

function light()
    print("light")
    if lighton == false then
        door.Transparency = 1
        lighton = true
    end
    if lighton == true then
        door.Transparency = 0
        lighton = false
    end
end

clickDetector.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.

local door = game.Workspace.RDoor
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = script.Parent 
local lighton = false

function light()
    print("light")
    if lighton then --Fig.A
        door.Transparency = 0
        lighton = false
    else --Fig.B
        door.Transparency = 1
        lighton = true
    end
end

clickDetector.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.

local door = game.Workspace.RDoor
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = script.Parent 
local lighton = 0

function light()
    print("light")
    if lighton == 0 then
        door.Transparency = 1
        lighton = 1

   else  if lighton == 1 then
        door.Transparency = 0
        lighton = 0
    end 
    end
end

clickDetector.MouseClick:Connect(light)

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

local door = game.Workspace.RDoor
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = script.Parent 


function light()

    if door.Transparency == 0 then
       door.Transparency = 1
    else 
       door.Transparency = 0
    end   
end

clickDetector.MouseClick:Connect(light)

or just 1 line of code using quick maths

local door = game.Workspace.RDoor
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = script.Parent 

function light()
door.Transparency = (door.Transparency -1)*-1
end

clickDetector.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