Zombie player detecting range!

  1. What do you want to achieve?
    I want to simplify my code (am so sorry if it’s so long I’m a beginner)

what my code does:
1)if player is in the zombie’s detection radius → move to them
2)if touch them → takes damage
if player die → move zombie back to start
3)if player walked out of radius zombie can detect → move zombie to start
I need help on number three
(code from line 9 to line 22)

basically, I want to know how to correctly detect when a player walks out of zombie’s detecting range
, thanks!

1 Like

Can you send the script ? i dont want to write it again lol ?

1 Like

oh okayy am opening Roblox studio 2 minutess plz

local ray = script.Parent.ray
local enemy = workspace.enemyFolder.enemy
ray.Touched:Connect(function(part)
	if part.Parent:FindFirstChild("Humanoid") and part.Parent.Parent ~= workspace.enemyFolder then
		enemy.Humanoid:MoveTo(part.Parent:FindFirstChild("HumanoidRootPart").Position)
	end
end)

local found = false
ray.TouchEnded:Connect(function()
	for i, p in pairs(ray:GetTouchingParts()) do
		if p and p:IsA("Part") and p.parent.parent ~= workspace.enemyFolder then
			if p.parent.Humanoid then
				found = true
			end			
		end
	end
	if found == false then
		enemy.Humanoid:MoveTo(workspace.aenemy.start.Position)
	end 
	found = false
end)
1 Like
local ray = script.Parent.ray
local enemy = workspace.enemyFolder.enemy

ray.Touched:Connect(function(part)
    if part.Parent:FindFirstChild("Humanoid") and part.Parent.Parent ~= workspace.enemyFolder then
        enemy.Humanoid:MoveTo(part.Parent:FindFirstChild("HumanoidRootPart").Position)
    end
end)

ray.TouchEnded:Connect(function()
    local found = false
    for _, p in pairs(ray:GetTouchingParts()) do
        if p.Parent.Parent ~= workspace.enemyFolder then
            found = true
            break
        end
    end

    if not found then
        enemy.Humanoid:MoveTo(workspace.aenemy.start.Position)
    end
end)

Try this and hope you tell me if it’s working !

1 Like

It works if I go in and leave the zombie’s detecting raduis but if I try walking around it the zombie starts shaking lol

Then it’s working correctly ? If yes make my answer on the top as the answer.

oh no. Do you have any better ways to detect when player go out of the zombie’s detecting raduis?

1 Like

Your zombie have HumanoidRootPart, i’m right ?

2 Likes

Stop trolling, try this as a template (MUST BE LOCALSCRIPT). Thanks.

local Player = game:GetService("Players").LocalPlayer
local Zombie = workspace.Zombie

local radius = 10

while true do
	local distance = (Player.Character.HumanoidRootPart.Position - Zombie.HumanoidRootPart.Position).Magnitude

	if distance <= radius then
		print("Player is within the radius of Zombie")
	else
		print("Player 1 is not within the radius of Zombie")
	end

	wait(0.1)
end
2 Likes

replace Player.Character.HumanoidRootPart.Position by Player.Character.Torso.Position if your game is in R6

1 Like


EDIT : should I change it to lower torso?

Your game is in R6 ? If not make it UpperTorso read the documentation

okay I edited your code and it works now tysm! :slight_smile:

local Player = game:GetService("Players").LocalPlayer
local Zombie = workspace.enemyFolder.enemy

local radius = 100

while true do
	local distance = (Player.Character:WaitForChild("UpperTorso").Position - Zombie.HumanoidRootPart.Position).Magnitude

	if distance <= radius then
		print("Player is within the radius of Zombie")
	else
		print("Player 1 is not within the radius of Zombie")
	end

	wait(0.1)
end

thanks for your time

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