Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

My script involving a onClick door is not working?

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.

2 comments

0

Did you use a local script for this?...

0

No.

1 answer

✓ Accepted answer

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.

2 comments

1

Thanks! This worked!

0

Cool

This discussion is preserved as it was. New answers, comments, and votes are closed.