I’m trying to make a script that checks if a player has a specific bool value set to true.
I want it so that if the bool value is set to true then it doesn’t damage the player, and if it’s false it does damage.
How would I achieve this?
This is what I have for the damage brick:
local damaging = false
script.Parent.Touched:connect(function(part)
if not damaging then
damaging = true
if part.Parent then
local human = part.Parent:FindFirstChild("Humanoid")
human.Health = human.Health - (human.MaxHealth/3)
end
wait(1)
damaging = false
end
end)
You can’t just assume that part.Parent has a humanoid child and use the variable normally, because other things can collide on your part. So I’ve made some changes to your code:
local damaging = false
script.Parent.Touched:connect(function(part)
local human = part.Parent:FindFirstChild("Humanoid")
if not damaging and human then
damaging = true
human:TakeDamage(human.MaxHealth/3)
--Same as "human.Health = human.Health - human.MaxHealth/3
wait(1)
damaging = false
end
end)
local damaging = false
script.Parent.Touched:connect(function(part)
if not damaging then
damaging = true
if part.Parent and part.Parent:GetAttribute("CanDamage") then
local human = part.Parent:FindFirstChild("Humanoid")
human.Health = human.Health - (human.MaxHealth/3)
end
wait(1)
damaging = false
end
end)
That would be the easiest way to add the check, this also allows you to set if a player can be damaged in another script. Beyond that there are some other things I would change, the 2 most notable ones being using task.wait() over wait() and switch to using a RunService.Heartbeat loop with GetPartsInPart or one of its relatives(the Touched event is simply not reliable enough to use).