#Problem with continously checking if an humanoid is touching range

1 messages · Page 1 of 1 (latest)

bright wadi
#

Hi, I have this one humanoid with a range and one without a range. I want the first one to keep moving along a path until its range touches a humanoid, and once the humanoid is dead/out of range I want it to stop shooting and keep walking but I just can't get it to stop shooting and keep walking. It also doesn't stop sometimes.

local unit = script.Parent
local humanoid = unit:FindFirstChild("Humanoid")
local hrp = unit:FindFirstChild("HumanoidRootPart")
local waypoints = game.Workspace.Waypoints
local currentWaypoint = 0
local currentDest = nil
local range = unit.Range
local debounce = false
local away = false

range.Position = unit.HumanoidRootPart.Position

function onTouched(target)
    local targethum = target.Parent:FindFirstChild("Humanoid")
    if targethum then
        away = false
        humanoid:MoveTo(hrp.Position)
        if not debounce then
            if not away then
                debounce = true
                while targethum.Health > 0 do
            
                    print("shooting")
                    targethum.Health -= 20
                    wait(1)
                
                end
            end
            debounce = false
        end
    end
end

local function onTouchEnded()
    away = true
    if currentDest ~= nil then
        print("continue moving")
        humanoid:MoveTo(currentDest)
    end
end

function moving()
    humanoid:MoveTo(waypoints[currentWaypoint].Position)
    currentDest = waypoints[currentWaypoint].Position
    print(currentDest)
    humanoid.MoveToFinished:Connect(function(success)
        if success and currentWaypoint < (#waypoints:GetChildren() - 1) then
            currentWaypoint += 1
            humanoid:MoveTo(waypoints[currentWaypoint].Position)
        end
        
    end)
    currentDest = nil
    --humanoid:MoveTo(waypoints[currentWaypoint].Position)
end
moving()
range.Touched:Connect(onTouched)
range.TouchEnded:Connect(onTouchEnded)

nova knot
#

@bright wadi why don't you just compare their distances every second or so?

bright wadi
#

wdym?

nova knot
#

to see if they're "touching"

bright wadi
#

I added a round circle hitbox

#

I want it to be touching that

#

would that work with distance

nova knot
bright wadi
#

can you give me an example of how you would do that?

nova knot
#

touched is pretty unreliable sometimes and you definitely don't need it for this

nova knot
#

like

#

is it always the same 2

bright wadi
nova knot
bright wadi
#

exactly

nova knot
#

alright then using .Touched is actualy a reasonable decision

#

but .TouchEnded is the problem bc its just very unreliable

#

You should combine the method I mentioned with .Touched

#

essentially

#

keep your .Touched

bright wadi
#

yeah?

nova knot
#

but instead of listening to .TouchEnded, do this: keep a global variable for the current target, which you also update in your .Touched function. Then, in a loop in the bottom of your script, check at a certain interval if the distance to your target is bigger than the radius of the range. if so, you can call your onTouchEnded function

bright wadi
nova knot
#

so put it at the top

bright wadi
#

oh ok, mb this is my first game

#

ok I will

hallow acorn
nova knot
#

wait wtf

#

where did my s2 go

#

??

hallow acorn
#

LMFAO

nova knot
#

no fr

#

why is it gone

#

😭

hallow acorn
#

just give yourself s2 back

#

u're a staff anyway

nova knot
hallow acorn
#

apply again then

nova knot
#

but why'd they take it bruh

#

thats so stupid

#

yk what

#

imma just get s3

#

im better than you s2s anyways

hallow acorn
#

dont worry

#

u are the same with us