Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Touch Function going crazy?

Asked by
Paldi 109
8 years ago

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)

01script.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
View all 23 lines...

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 years ago
Edited 8 years ago

You can add a Debounce to your script to prevent unnecessary firing of events.

01local db = false; --It is off
02 
03script.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
View all 31 lines...
1
now it don't work at all for some reasons xD Paldi 109 — 8y
Ad

Answer this question