Hello, i'm trying to make some kind of hitbox to use as a zone in a kind of Capture the zone game and when i touch it it works as expected but when i leave it it dont, when i touch it, it should add +1 to the "Hero" or "villain" value and when i leave it should negate -1 to it but what happen is it negate like -40 at once. (i left some notes bellow)
01 | script.Parent.Touched:connect( function (hit) |
02 | local hero = script.Parent.Hero --this is a number value |
03 | local villain = script.Parent.Villain --^ |
04 | if hit.Name = = "Torso" then |
05 | local plr = game.Players:GetPlayerFromCharacter(hit.Parent) |
06 | if plr then |
07 | local side = game.Lighting.Players:FindFirstChild(plr.Name).Side -- this is a string value |
08 | if side.Value = = "Hero" then |
09 | hero.Value = hero.Value+ 1 |
10 | elseif side.Value = = "Villain" then |
11 | villain.Value = villain.Value+ 1 |
12 | end |
13 | script.Parent.TouchEnded:connect( function () |
14 | local side = game.Lighting.Players:FindFirstChild(plr.Name).Side |
15 | if side.Value = = "Hero" then |
You can add a Debounce to your script to prevent unnecessary firing of events.
01 | local db = false ; --It is off |
02 |
03 | script.Parent.Touched:connect( function (hit) |
04 | if not db then --if it is off |
05 | db = true ; --turn it on, and continue w/ code |
06 | local hero = script.Parent.Hero |
07 | local villain = script.Parent.Villain |
08 | if hit.Name = = "Torso" then |
09 | local plr = game.Players:GetPlayerFromCharacter(hit.Parent) |
10 | if plr then |
11 | local side = game.Lighting.Players:FindFirstChild(plr.Name).Side |
12 | if side.Value = = "Hero" then |
13 | hero.Value = hero.Value+ 1 |
14 | elseif side.Value = = "Villain" then |
15 | villain.Value = villain.Value+ 1 |