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)
script.Parent.Touched:connect(function(hit) local hero = script.Parent.Hero --this is a number value local villain = script.Parent.Villain --^ if hit.Name == "Torso" then local plr = game.Players:GetPlayerFromCharacter(hit.Parent) if plr then local side = game.Lighting.Players:FindFirstChild(plr.Name).Side -- this is a string value if side.Value == "Hero" then hero.Value = hero.Value+1 elseif side.Value == "Villain" then villain.Value = villain.Value+1 end script.Parent.TouchEnded:connect(function() local side = game.Lighting.Players:FindFirstChild(plr.Name).Side if side.Value == "Hero" then hero.Value = hero.Value-1 elseif side.Value == "Villain" then villain.Value = villain.Value-1 end end) end end end)
You can add a Debounce to your script to prevent unnecessary firing of events.
local db = false; --It is off script.Parent.Touched:connect(function(hit) if not db then --if it is off db = true; --turn it on, and continue w/ code local hero = script.Parent.Hero local villain = script.Parent.Villain if hit.Name == "Torso" then local plr = game.Players:GetPlayerFromCharacter(hit.Parent) if plr then local side = game.Lighting.Players:FindFirstChild(plr.Name).Side if side.Value == "Hero" then hero.Value = hero.Value+1 elseif side.Value == "Villain" then villain.Value = villain.Value+1 end script.Parent.TouchEnded:connect(function() if db then --If it is on(like it should be if touched) local side = game.Lighting.Players:FindFirstChild(plr.Name).Side if side.Value == "Hero" then hero.Value = hero.Value-1 elseif side.Value == "Villain" then villain.Value = villain.Value-1 end db = false; --Turn back off end end) end end end end)