Problem of door script

I created a script for the doors, but the door opens too often with repeated touch. To fix this, I added a bool variable, but it doesn’t help.

my script:
local bool = true
function onTouched(hit)
local playerlol = hit.Parent.Name
if game.Players[playerlol].Backpack:FindFirstChild(“KeyCard”) or hit.Parent:FindFirstChild(“KeyCard”) and bool then
bool = false
script.Parent.Sound:Play()
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”)
wait(8)
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”)
bool = true
end
end
script.Parent.Touched:connect(onTouched)

structure: image
Vedo of the problem: robloxapp-20210208-2006379.wmv (2.0 MB)

1 Like

Can you give me an image of the error and when you click it, what line does it direct you to?

image

Also in your code for the if statement, instead of just saying bool, say

if game.Players[playerlol].Backpack:FindFirstChild(“KeyCard”) or hit.Parent:FindFirstChild(“KeyCard”) and bool == true then
bool = false
script.Parent.Sound:Play()
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”)
wait(8)
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”)
bool = true
end
end

script.Parent.Touched:connect(onTouched)

In your code, you didn’t ask whether bool was true or false so I’m assuming that is where the error is. (In your if statement)

Some mesh objects are descendants of our players main mesh. Say the handle in my hat. The way to fix this is to simply check if our hit.Parent is a class model

Try making “bool” global. So, instead of this on the first line:

local bool = false

try using this:

bool = false

And as for the second issue, do this:

Instead of:

if game.Players[playerlol].Backpack:FindFirstChild(“KeyCard”) or hit.Parent:FindFirstChild(“KeyCard”)

Try doing:

if game.Players:FindFirstChild(playerlol) or hit.Parent:FindFirstChild(“KeyCard”) then
if game.Players[playerlol].Backpack:FindFirstChild(“KeyCard”) or hit.Parent:FindFirstChild(“KeyCard”)

Don’t forget to add another “end” at the bottom!

1 Like

robloxapp-20210208-2017407.wmv (1.3 MB)

1 Like

Do this instead, I forgot to include the detection for the bool.

if game.Players:FindFirstChild(playerlol) or hit.Parent:FindFirstChild(“KeyCard”) and bool then
if game.Players[playerlol].Backpack:FindFirstChild(“KeyCard”) or hit.Parent:FindFirstChild(“KeyCard”) then

image What do the red lines mean?

Did you include the end at the bottom that I mentioned? To see what the red is, there should be text at the bottom left of your script that says the error.

That code actually hurts to look at AAAAAAAAAA

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

                for _, Part in pairs(script.Parent.Parent:GetDescendants()) do
                    if Part:IsA("BasePart") or Part:IsA("Union") then
                       Part.Transparency = 1
                       Part.CanCollide = false
                    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("Union") then
                        Part.Transparency = 0
                        Part.CanCollide = true
                    end
                end

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

script.Parent.Touched:connect(onTouched)

You might wanna work on formatting your code ack, anyways I took the time to make a more simple version to try if it works

3 Likes

See it in Script Analysis.

1 Like

Should figured that was coming
Ok, lemme hop onto studio real quick to fix it then aaaa

1 Like

ok. i will create new place. Wait a minute

You- Didn’t- Need- To- Make- One ._.
Ok try this?

local bool = true
function onTouched(hit)
	if hit.Parent then
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if Player then
			if Player.Backpack:FindFirstChild("KeyCard") or hit.Parent:FindFirstChild("KeyCard") and bool == true 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
						Part.Transparency = 1
						Part.CanCollide = false
					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
						Part.Transparency = 0
						Part.CanCollide = true
					end
				end

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

script.Parent.Touched:connect(onTouched)

Better send the descendants of the model and so we make a shorter script.

Weirdly enough I get confused with Unions & UnionOperations (Which is probably gonna be a bad habit for me ahaha)

door.rbxm (12.8 KB)