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

Door Open/Close effect on Click?

Asked by 4 years ago

I am currently creating a puzzle game where you move boxes on squares to open doors, but for now I'm creating a door which you click to open. Here is my code:

doorPart = script.Parent
clickDetector = doorPart.ClickDetector

doorOpen = false


clickDetector.MouseClick:connect(function()
    if (doorOpen == false) then
        doorOpen = true
    else if (doorOpen == true) then
            doorOpen = false
        end

    end

end)

if (doorOpen == true) then
    doorPart.CanCollide = false
    doorPart.Transparency = .5
    else if(doorOpen == false) then
            doorPart.CanCollide = true
            doorPart.Transparency = 0
        end
end

It used to be one big if, where it would change the CanCollide, Transparency, and doorOpen variable all at once, but that didn't work so I attempted to make 2 seperate conditional statements.

Help is greatly appreciated! Thanks!

:)

4 answers

Log in to vote
0
Answered by
Uzixt 19
4 years ago
Edited 4 years ago

The problem is that your code below doesn't run when you click the ClickDetector. It only runs once when the game starts. To fix this, you can just wrap the part of your code in a while loop. Eg

doorPart = script.Parent
clickDetector = doorPart.ClickDetector

doorOpen = false


clickDetector.MouseClick:connect(function()
    if (doorOpen == false) then
        doorOpen = true
    else if (doorOpen == true) then
            doorOpen = false
        end

    end

end)

while true do wait()
if (doorOpen == true) then
    doorPart.CanCollide = false
    doorPart.Transparency = .5
else if(doorOpen == false) then
         doorPart.CanCollide = true
            doorPart.Transparency = 0
        end
    end
end

This code will check the doorOpen value every 1/30th of a second.

0
That's not how clickdetectors work, so the script wouldn't work. ParticularlyPenguin 71 — 4y
0
Its not the full script... Uzixt 19 — 4y
0
If you use a built in function (game.Workspace.Part.ClickDetector.MouseClick:Connect(function())) then it detects it every single time, not just the second the server starts. ParticularlyPenguin 71 — 4y
0
Oh! I apologize. I see what you did now. I'd suggest you do a 'while wait() do' loop as it looks more neat. ParticularlyPenguin 71 — 4y
View all comments (3 more)
0
Yeah ik that the thing is that the code from line 17 and below doesn't update when he uses the ClickDetector because its not in the clickDetector function, meaning you just have to use a loop /: Uzixt 19 — 4y
0
your script worked now on a standard script ;D but why would it be a Script and not a localScript? if we're getting the players mouse, isn't that client sided? wolffdonnaven 12 — 4y
0
it is a local script Uzixt 19 — 4y
Ad
Log in to vote
0
Answered by
ACHRONlX 255 Moderation Voter
4 years ago

Try something like this:

local dp = script.Parent
local cd = dp.ClickDetector

do = false

cd.MouseClick:Connect(function()
if do then
dp.CanCollide = false
dp.Transparency = 0
do = false
elseif do = false then
dp.CanCollide = true
dp.Transparency = true
do = true
end
end)

Sorry for any mistakes, I'm on mobile right now.

0
Don't just "Spoon-Feed" people Luka_Gaming07 534 — 4y
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

The correct script:

doorPart = script.Parent
clickDetector = doorPart.ClickDetector

doorOpen = false

clickDetector.MouseClick:connect(function()
    if doorOpen == false then
        doorOpen = true
    elseif doorOpen == true then
        doorOpen = false
    end
end)

while wait() do
    if doorOpen == true then
        doorPart.CanCollide = false
        doorPart.Transparency = .5
    elseif doorOpen == false then
        doorPart.CanCollide = true
        doorPart.Transparency = 0
    end
end

First off, you don't need those odd parenthesis when doing an if statement. Second off, for elseif statements, you don't need an extra 'end' because it's still part of the if statement that it's under (hope that makes sense). Third off, you need to do a "while wait() do" loop for your if statement, as it won't continuously check like a built in function (a "while wait() do" loop is an effective way to check consistently and keep the script from crashing),

0
Uhm this doesn't work.. have you tested this in studio? Uzixt 19 — 4y
0
Fixed ^ ParticularlyPenguin 71 — 4y
0
/: Uzixt 19 — 4y
0
Thank you! I will try this and see if it works wolffdonnaven 12 — 4y
View all comments (4 more)
0
this didn't work, im not sure why? the game is locked in first person, but im not sure if that would make a difference? wolffdonnaven 12 — 4y
0
bruh he copied my answer but it should work, is this in a Server Script? Uzixt 19 — 4y
0
2nd, as stated on the dev Forums, you need to do while true do wait(), because it is better practise. Try my script above Uzixt 19 — 4y
0
why would this be in a ServerScript? i get my scripts mixed up, and im not sure where to use a local, normal, module, server, script wolffdonnaven 12 — 4y
Log in to vote
0
Answered by 4 years ago

Another way to do this, without having loops, is to put all of that code inside of the clickDetector.mouseClick function. Here is how I would script a simple door:


-- Stuff local doorPart = script.Parent local clickDetector = doorPart.ClickDetector -- Vars local doorOpen = false -- Internal Functions local function ToggleDoor() if doorOpen then -- Close the door doorPart.CanCollide = true doorPart.Transparency = 0 else -- Open the door doorPart.CanCollide = false doorPart.Transparency = 0.5 end end) -- Click detector clickDetector.MouseClick:Connect(function() doorOpen = not doorOpen -- Set the doorOpen to whatever it's not. ToggleDoor() -- Apply the changes. Also, if it's your preference, you can just copy the function's code and re-paste it here if you don't want another separate function. end)

This is better, because if you decide to duplicate the door for other parts of your game, you won't have many loops constantly running and checking for door's state. (If you want to be even more efficient you could use CollectionService tags and then you can have a single script control all doors.)

Hope this helps!

0
even tho i already fixed it thanks for answering ;) wolffdonnaven 12 — 4y

Answer this question