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

My script involving a onClick door is not working?

Asked by 8 years ago
Edited 8 years ago
touched = true
Door = game.Workspace.DoorPart

function onClick()
    if touched == false then
        Door.BrickColor = BrickColor.new("White")
        Door.CanCollide = false
        touched = true
    else
        Door.BrickColor = BrickColor.new("Brown")
        Door.CanCollide = true
        touched = false
    end
end

script.Parent.Parent.ClickDetector.MouseClick:connect(onClick)

In studio, this script works perfectly, but it doesn't work using the Roblox Player. I don't see anything in the output either.

0
Did you use a local script for this?... StoIid 364 — 8y
0
No. greenyboy4164821 30 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

I only see two possible problems

  • #1,

The first problem could be that you're not waiting for objects to load. Using WaitForChild would fix this.

touched = true
Door = game.Workspace:WaitForChild("DoorPart")

function onClick()
    if touched == false then
        Door.BrickColor = BrickColor.new("White")
        Door.CanCollide = false
        touched = true
    else
        Door.BrickColor = BrickColor.new("Brown")
        Door.CanCollide = true
        touched = false
    end
end

script.Parent.Parent:WaitForChild("ClickDetector").MouseClick:connect(onClick)
  • #2,

The second problem I see that could be happening could be that you're putting the script somewhere wrong. Place a regular script in ServerScriptService or something.


Optimization

We actually don't need the variable, we can just check if the door's CanCollide or not.

local Door = game.Workspace:WaitForChild("DoorPart")

function onClick()
    if Door.CanCollide then
        Door.BrickColor = BrickColor.new("White")
        Door.CanCollide = false
    else
        Door.BrickColor = BrickColor.new("Brown")
        Door.CanCollide = true
    end
end

script.Parent.Parent:WaitForChild("ClickDetector").MouseClick:connect(onClick)

I would also add a debounce so the script doesn't spam out.

local Door = game.Workspace:WaitForChild("DoorPart")
local DB = false

function onClick()
    if DB then return end
    DB = true
    if Door.CanCollide then
        Door.BrickColor = BrickColor.new("White")
        Door.CanCollide = false
    else
        Door.BrickColor = BrickColor.new("Brown")
        Door.CanCollide = true
    end
    wait(2)
    DB = false
end

script.Parent.Parent:WaitForChild("ClickDetector").MouseClick:connect(onClick)

Let me know if any of that worked for you.

Good Luck!

If I did help, please don't forget to accept my answer.
1
Thanks! This worked! greenyboy4164821 30 — 8y
0
Cool User#11440 120 — 8y
Ad

Answer this question