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