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