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