Distance to the part

Because the zombie left the part so the Touched event will stop working

Yes, i understand this, but how i can track this?

What is the base part in your example? My rectangle?

How i know raycast goes to the middle, but not to the closest point?

(dont mind the poorly formatted code this is just a demo)

local HitPart = workspace.HitPart
local Char = script.Parent

local Hum, Root = Char:WaitForChild("Humanoid"), Char:WaitForChild("HumanoidRootPart")

task.spawn(function()
	while true and task.wait(1/33) do
		local Params = RaycastParams.new()
		Params.FilterType = Enum.RaycastFilterType.Exclude
		Params.FilterDescendantsInstances = {Char}
		
		local Cast = workspace:Raycast(Root.Position, Root.CFrame.LookVector*3, Params)
		
		if (Cast) and (Cast.Instance == HitPart) then 
			HitPart.Color = Color3.fromRGB(255, 0, 0)
			Hum.WalkSpeed = 0
			print("Attacking")
		else 
			HitPart.Color = Color3.fromRGB(0, 255, 0)
			Hum.WalkSpeed = 16
			Hum:MoveTo(HitPart.Position)
			print("Moving")
		end
	end
end)
1 Like

I recommend using RunService.Heartbeat or smth related to RunService because when there is a lot of zombies (a lot of computing), game might lag and while loop will slow down I think

Yup, and it’ll return the point that’s closest. Take the distance from the zombie to that point.

1 Like

I tried it, and i think i have some troubles, check this video bc im bad with raycasts.
https://youtu.be/vf5lyqAb9P0 (its still loading, give it some time)

Here is your code (i just added print, maybe i copied wrong) :

local function test1()
local HitPart = workspace.Map.Truck.HealthGUI
local Char = script.Parent

local Hum, Root = Char:WaitForChild("Humanoid"), Char:WaitForChild("HumanoidRootPart")

task.spawn(function()
	while true and task.wait(1/33) do
		local Params = RaycastParams.new()
		Params.FilterType = Enum.RaycastFilterType.Exclude
		Params.FilterDescendantsInstances = {Char}

		local Cast = workspace:Raycast(Root.Position, Root.CFrame.LookVector*3, Params)
		
		print(Cast)
		
		if (Cast) and (Cast.Instance == HitPart) then 
			HitPart.Color = Color3.fromRGB(255, 0, 0)
			Hum.WalkSpeed = 0
			print("Attacking")
		else 
			HitPart.Color = Color3.fromRGB(0, 255, 0)
			Hum.WalkSpeed = 16
			Hum:MoveTo(HitPart.Position)
			print("Moving")
		end
	end
end)

end


And im calling it before my pathfinding code

BasePart:GetClosestPointOnSurface is not enabled yet
I wrote something like this : workspace.Map.Truck.PrimaryPart:GetClosestPointOnSurface(script.Parent.PrimaryPart.Position)

14:35:36.092 > print(workspace.SpawnLocation:GetClosestPointOnSurface(Vector3.new(10,0,0))) - Studio
14:35:36.094 6, 0, 0 - Edit

1 Like

tried to add spawn, but still same error

You misunderstand what I’m doing here. I’m printing the closest point of SpawnLocation’s surface, in relation to Vector3.new(10,0,0)

Apologies for forgetting to reply, what do you want to track?

Yes, i understand this, but i have error BasePart:GetClosestPointOnSurface is not enabled yet and i just cant do it

I need to track when zombie next to the my rectangle. I think raycast really good solution, but i really bad understand how to use them, now im trying to understand

What do you mean by track? Do you mean that you want to check if the zombie left the part?

Yes, I want to check if zombie left the part and if zombie entered

If the zombie is chasing the player, won’t that count as the zombie leaving the part?

Yes, it will count as the zombie leaving the part, bc player can just run away

Yea, you can do that to check when zombie leave the part