KeyCard door | help

I have a script that I really like but it has a bug. If you shoot at the door that this script opens, then it stops working.

My script:

local bool = true

function onTouched(hit)
	if hit.Parent and bool == true then
		bool = false
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if Player then
			if Player.Backpack:FindFirstChild("KeyCard") or hit.Parent:FindFirstChild("KeyCard") then
				script.Parent.Sound:Play()

				for _, Part in pairs(script.Parent.Parent:GetDescendants()) do
					if Part:IsA("BasePart") or Part:IsA("UnionOperation") then
						script.Parent.Parent.Union.Transparency = 1
						script.Parent.Parent.Model.Part1.Transparency = 1
						script.Parent.Parent.Model.Part2.Transparency = 1
						script.Parent.Parent.Model.Part3.Transparency = 1
						script.Parent.Parent.Model.Part4.Transparency = 1
						script.Parent.Parent.Union.CanCollide = false
						script.Parent.Parent.Model.Part1.CanCollide = false
						script.Parent.Parent.Model.Part2.CanCollide = false
						script.Parent.Parent.Model.Part3.CanCollide = false
						script.Parent.Parent.Model.Part4.CanCollide = false
						script.Parent.Parent.Light.BrickColor = BrickColor.new("Lime green")
					end
				end

				script.Parent.Parent.Light.BrickColor = BrickColor.new("Lime green")
				wait(8)

				for _, Part in pairs(script.Parent.Parent:GetDescendants()) do
					if Part:IsA("BasePart") or Part:IsA("UnionOperation") then
						script.Parent.Parent.Union.Transparency = 0
						script.Parent.Parent.Model.Part1.Transparency = 0
						script.Parent.Parent.Model.Part2.Transparency = 0
						script.Parent.Parent.Model.Part3.Transparency = 0
						script.Parent.Parent.Model.Part4.Transparency = 0
						script.Parent.Parent.Union.CanCollide = true
						script.Parent.Parent.Model.Part1.CanCollide = true
						script.Parent.Parent.Model.Part2.CanCollide = true
						script.Parent.Parent.Model.Part3.CanCollide = true
						script.Parent.Parent.Model.Part4.CanCollide = true
						script.Parent.Parent.Light.BrickColor = BrickColor.new("Really red")
					end
				end

				script.Parent.Parent.Light.BrickColor = BrickColor.new("Really red")
				bool = true
			end
		end
	end
end

script.Parent.Touched:connect(onTouched)

Video: robloxapp-20210209-0914233.wmv (3.0 MB)

Structure: image

1 Like

From what I see it tries to find the bullets parent in Players, but I don’t know if that errors the script or just makes the variable you put nill

So how fix this? Help pls. I really need it

Can you show me the output please?

I was wrong. It stops working if you touch the door with a weapon.

robloxapp-20210209-0934498.wmv (1.7 MB)

Oh, well same thing almost, it’s the handle of the weapon touching the door, so hit.parent is the tool, So it’s trying to find the tool in Players

But why does it stop working? I don’t know what to do about it

Check if a Player named hit.Parent.Name exists in Players before making it a variable.

Try this in lines 6 and 7 where you made a variable where you tried to find something(a player) named hit.Parent in Players which would error if it wasn’t a child of the physical player.

Can you help me? I do not know how to do that

local bool = true

function onTouched(hit)
    if hit.Parent and bool == true then
        bool = false
        
        if game.Players:GetPlayerFromCharacter(hit.Parent.Name) then
local Player = game.Players:GetPlayerFromCharacter(hit.Parent.Name)
            if Player.Backpack:FindFirstChild("KeyCard") or hit.Parent:FindFirstChild("KeyCard") then
                script.Parent.Sound:Play()

                for _, Part in pairs(script.Parent.Parent:GetDescendants()) do
                    if Part:IsA("BasePart") or Part:IsA("UnionOperation") then
                        script.Parent.Parent.Union.Transparency = 1
                        script.Parent.Parent.Model.Part1.Transparency = 1
                        script.Parent.Parent.Model.Part2.Transparency = 1
                        script.Parent.Parent.Model.Part3.Transparency = 1
                        script.Parent.Parent.Model.Part4.Transparency = 1
                        script.Parent.Parent.Union.CanCollide = false
                        script.Parent.Parent.Model.Part1.CanCollide = false
                        script.Parent.Parent.Model.Part2.CanCollide = false
                        script.Parent.Parent.Model.Part3.CanCollide = false
                        script.Parent.Parent.Model.Part4.CanCollide = false
                        script.Parent.Parent.Light.BrickColor = BrickColor.new("Lime green")
                    end
                end

                script.Parent.Parent.Light.BrickColor = BrickColor.new("Lime green")
                wait(8)

                for _, Part in pairs(script.Parent.Parent:GetDescendants()) do
                    if Part:IsA("BasePart") or Part:IsA("UnionOperation") then
                        script.Parent.Parent.Union.Transparency = 0
                        script.Parent.Parent.Model.Part1.Transparency = 0
                        script.Parent.Parent.Model.Part2.Transparency = 0
                        script.Parent.Parent.Model.Part3.Transparency = 0
                        script.Parent.Parent.Model.Part4.Transparency = 0
                        script.Parent.Parent.Union.CanCollide = true
                        script.Parent.Parent.Model.Part1.CanCollide = true
                        script.Parent.Parent.Model.Part2.CanCollide = true
                        script.Parent.Parent.Model.Part3.CanCollide = true
                        script.Parent.Parent.Model.Part4.CanCollide = true
                        script.Parent.Parent.Light.BrickColor = BrickColor.new("Really red")
                    end
                end

                script.Parent.Parent.Light.BrickColor = BrickColor.new("Really red")
                bool = true
            end
        end
    end
end

script.Parent.Touched:connect(onTouched)

Sorry if it took too long, it is very hard to type reply’s on mobile

It appears you have your debounce in the wrong place so it wasn’t being set to true, causing it stop running. Here’s your updated code:

local bool = true

function onTouched(hit)
	if hit.Parent and bool == true then
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if Player then
			if Player.Backpack:FindFirstChild("KeyCard") or hit.Parent:FindFirstChild("KeyCard") then
				bool = false
				script.Parent.Sound:Play()

				for _, Part in pairs(script.Parent.Parent:GetDescendants()) do
					if Part:IsA("BasePart") or Part:IsA("UnionOperation") then
						script.Parent.Parent.Union.Transparency = 1
						script.Parent.Parent.Model.Part1.Transparency = 1
						script.Parent.Parent.Model.Part2.Transparency = 1
						script.Parent.Parent.Model.Part3.Transparency = 1
						script.Parent.Parent.Model.Part4.Transparency = 1
						script.Parent.Parent.Union.CanCollide = false
						script.Parent.Parent.Model.Part1.CanCollide = false
						script.Parent.Parent.Model.Part2.CanCollide = false
						script.Parent.Parent.Model.Part3.CanCollide = false
						script.Parent.Parent.Model.Part4.CanCollide = false
						script.Parent.Parent.Light.BrickColor = BrickColor.new("Lime green")
					end
				end

				script.Parent.Parent.Light.BrickColor = BrickColor.new("Lime green")
				wait(8)

				for _, Part in pairs(script.Parent.Parent:GetDescendants()) do
					if Part:IsA("BasePart") or Part:IsA("UnionOperation") then
						script.Parent.Parent.Union.Transparency = 0
						script.Parent.Parent.Model.Part1.Transparency = 0
						script.Parent.Parent.Model.Part2.Transparency = 0
						script.Parent.Parent.Model.Part3.Transparency = 0
						script.Parent.Parent.Model.Part4.Transparency = 0
						script.Parent.Parent.Union.CanCollide = true
						script.Parent.Parent.Model.Part1.CanCollide = true
						script.Parent.Parent.Model.Part2.CanCollide = true
						script.Parent.Parent.Model.Part3.CanCollide = true
						script.Parent.Parent.Model.Part4.CanCollide = true
						script.Parent.Parent.Light.BrickColor = BrickColor.new("Really red")
					end
				end

				script.Parent.Parent.Light.BrickColor = BrickColor.new("Really red")
				bool = true
			end
		end
	end
end

script.Parent.Touched:connect(onTouched)
1 Like

this does not work. Should I change something in the structure?

It still stops working if the gun touches it?

Ohhhhh ValueKing is right. The debounce was set to false even it wasn’t touched by a player

And was only set back to true if the thing who touched it was a child of the physical character. When it was touches by the gun handle, The denounced turned to false and never turned back to true

By debounce I mean the variable called bool

This script helped me. thanksThis script helped me. thanks

3 Likes