Door that kills player when closed not functioning

be amazed by my garbage code
I just have a question concerning this door i tried to make
The idea is that when the door is open, the player can pass through freely but when closed, the player will die.
There are two diffrent scripts, one for a button that triggers the “doordanger” value to be = true if closed


And another script for the door itself that is supposed to kill the player

the door does open and close but the killing of the player does not happen, please help

and here are my god tier modeling skills


the “door” and button

This is because this is a separate script which won’t detect if “doordanger” is true or false.

Here is how you might go about fixing this:

local Door = game.Workspace.Door
local ClickDetector = Door.ClickDetector

local DoorDanger = false

ClickDetector.MouseClick:Connect(function()
	DoorDanger = false
	Door.Transparency = 0.5
	Door.CanCollide = false
	wait(5)
	Door.Transparency = 0
	DoorDanger = true
end)

Door.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and DoorDanger then
		hit.Parent:FindFirstChild("Humanoid"):TakeDamage(100)
	end
end)
1 Like

Okay so first of all, make it one script to prevent clutter in the workspace.
Second of all, “global” variables aren’t as global as you thought. You can’t use the same variable across 2 scripts. Third of all, put if doordanger == true then inside of the touched event so i can be fired. Fourth of all, I don’t understand why you put this is startercharacter, and you also only need 1 “parent” on line 3, there are many more errors but i dont have all day. Next time, please consider watching a few tutorials before going to the dev forum. (in case your wondering, the difference between a global and local variable is, when a variable is local, it can only be used inside the area its in. For example, a function, if statement ect. Because the script itself is a public place, you can put a local variable at line one and it can be used anywhere. A global variable must be defined outside of a function/if statement and can be changed inside of functions/if statements.

1 Like

just make it only 1 script and it will work

1 Like