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
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.
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()