✓ Accepted answer
I only see two possible problems
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.
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
Did you use a local script for this?...
No.