Touched doesnt work they way it was intended

The way i want it to work is when an NPC called “Enemy”
so when i spawn, NPC automatically walks to part named ‘signal’
but when it touches the signal for some reason it doesnt count as Touched
here is the video if you dont understand what i say (i hope it works)

here is Touched script

local part = workspace.DMGAble.signal

	part.Touched:Connect(function(hit)
		print(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			if hit.Name == 'Hitbox' then
				if hit.Parent:FindFirstChild('totallynotd') then
					game.ReplicatedStorage.Damage:FireServer(hit.Parent.totallynotd.Value)
				end
				
			end
			end
			
			
			

		
	end)

	

and here is a pathfinding script (if that helps)

local humanoid = script.Parent:WaitForChild("Humanoid")
local signalPart = game.Workspace.DMGAble.signal
local walking = true

local function walkToSignal()
	if walking then
		humanoid:MoveTo(signalPart.Position)
	end
end

while true do
	walkToSignal()
	wait(0.1) 
	if humanoid.WalkToPart == signalPart then
		
		walking = false
	end
end

Does your enemy have a humanoid? The humanoid in an enemy, If it doesn’t have, It wont work

1 Like

Truth be told, roblox .Touched event is horrible, dont know how it works, but its bad.
Maybe just send a bindable event telling it that you’ve reached that area? Or you could use workspace:GetPartsBoundInBox() or you can check the distance between them and if its close enough count it.

1 Like

yes it does have one, not quite sure why that doesnt work

1 Like

sure. could you tell me how to do that? i am not that good at scripting

1 Like

Wait actually just thought of something, once the pathfinding is complete, just do whatever there, what whatever is can vary.
So, is the script that does the .Touched a local script or a script (server script)?

does your part has CanCollide set to true? bc if its false your npc can pass through it without triggering the Touched event. you also need to check if the signal part is not set to massless or anchored bc that can prevent proper collision detection. also the MoveTo function doesnt really bring the NPC directly in contact with the part, you could use a function like this humanoid:MoveTo(signalPart.Position, signalPart)
or you could use the Magnitude method for the distance between yhe NPC and the singal part and then treat that as a “contact”

local distance = (signalPart.Position - npc.HumanoidRootPart.Position).Magnitude
if distance < 3 then 
    print("NPC reached the signal")
    -- this triggers the same event you'd use if it was touched.
end

okay now i give you the full script that is fixed up

pathfinding script:

local humanoid = script.Parent:WaitForChild("Humanoid")
local signalPart = game.Workspace.DMGAble.signal

local function walkToSignal()
    humanoid:MoveTo(signalPart.Position, signalPart)
end

-- this event triggers when the NPC reaches the destination or if it cannot reach it
humanoid.MoveToFinished:Connect(function(reached)
    if reached then
        print("NPC has reached the signal part")
    else
        print("NPC couldn't reach the part, trying again...")
        -- this retries the move if it somehow fails
        walkToSignal()
    end
end)

--  this starts the walking towards the signal part
walkToSignal()

touched script:

local part = workspace.DMGAble.signal

part.Touched:Connect(function(hit)
    print("Part touched by: ", hit:GetFullName())
    if hit and hit.Parent:FindFirstChild("Humanoid") then
        if hit.Name == 'Hitbox' then
            local totallynotd = hit.Parent:FindFirstChild('totallynotd')
            if totallynotd then
                print("Firing server for damage:", totallynotd.Value)
                game.ReplicatedStorage.Damage:FireServer(totallynotd.Value)
            end
        end
    end
end)

Yeah, this is more reliable than using the touched event

so like it’s more of something like this

local enemy = script.Parent
local humanoid = enemy:WaitForChild("Humanoid")
local signalPart = game.Workspace.DMGAble.signal

local function walkToSignal()
    humanoid:MoveTo(signalPart.Position, signalPart)
end

humanoid.MoveToFinished:Connect(function(reached)
    if reached then
        print("Enemy has reached the signal part")
    else
        print("NPC couldn't reach the part, retrying...")
        walkToSignal()
    end
end)

walkToSignal()
1 Like

Thanks! I will try that when i get back home

Alright, i have tried using this method but it always retries
Probably because the signal should be anchored

Local script, it fires the server event

alright, i made a little ‘signal hitbox’ so npc can touch it instead and i guess it works?

1 Like

Why did you solution yourself?

alright i will solution you then
such a shame i cant do two solutions

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.