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.
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)
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.
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!