So I have a script inside of a part called 'Glass'
Whenever something touches it with a Parent named 'Zombie' it takes away one from a value called 'Health' in the Part.
The script works fine, if the zombie touches it first. But if the player touches it first, then the zombie won't make any change to it.
I think I know how to fix this, but I'm not sure. I think I have to use 'else return' somewhere, but everywhere i tried to put it it didn't work.
This is the script inside the glass part:
local db = 0 local health = script.Parent:WaitForChild("Health") function onTouched(hit) local player = game.Players:GetPlayerFromCharacter(hit.Parent) local human = hit.Parent:findFirstChild("Humanoid") if db == 0 then db = 1 if hit.Parent.Name == "Zombie" then health.Value = health.Value - 1 print("ow") print(health.Value) wait(math.random(1,3)) db = 0 end end end script.Parent.Touched:connect(onTouched)
I don't think you need to use "return" for this script. I believe the problem occurs when you change the value of db.
if db == 0 then db = 1 if hit.Parent.Name == "Zombie" then health.Value = health.Value - 1 print("ow") print(health.Value) wait(math.random(1,3)) db = 0 end end
Notice how regardless of who touches the block, db becomes 1. This means that non-zombies can change db's value to 1. However, only zombies can change the db value to 0! So when a non-zombie touches the part, it turns db to 1 and doesn't change it back. This causes the if statement to fail.
if db == 0 then if hit.Parent.Name == "Zombie" then db = 1 health.Value = health.Value - 1 print("ow") print(health.Value) wait(math.random(1,3)) db = 0 end end
Simply move the line of code inside the if statement. Now non-zombies won't change the db value!
~~~~~~~~~~~~~~~~~ local db = 0 local health = script.Parent.Health
script.Parent.Touched:connect(function(h)
if db == 0 then
if h.Parent.Name == "Zombie" then
db = 1
health.Value = health.Value - 1
print("OW!")
print(health.Value)
wait(math.random(1, 3))
db = 0
end
end
end)
~~~~~~~~~~~~~~~
You can add back in your variables, but this functions regardless of what touches the block first. (Tested by making a clone of myself named 'zombie' touch the brick after I had.