You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
A flying script where the npc stops if theres a wall infront of it -
What is the issue? Include screenshots / videos if possible!
The raycast returns a part which is well out of its supposed range. -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have sifted through lots of dev forum posts but i dont see any solutions
My flying script:
local npc = script.Parent
local hrpOfNPC = npc:WaitForChild("HumanoidRootPart")
local humanoid = npc:WaitForChild("Humanoid")
local plrsHit = {}
local maxDistance = math.huge
local pursuitCooldown = 0
local RP = RaycastParams.new()
RP.FilterDescendantsInstances = {npc}
RP.FilterType = Enum.RaycastFilterType.Exclude
npc.Humanoid.Touched:Connect(function(touch)
if humanoid.Health >= 1 then
local player = game.Players:GetPlayerFromCharacter(touch.Parent)
if player and not plrsHit[player] then
plrsHit[player] = true
touch.Parent.Humanoid:TakeDamage(8)
wait(1)
plrsHit[player] = false
end
end
end)
local function getClosestPlayer()
local plrs = game.Players:GetPlayers()
local closestHRP
local closestDistance = math.huge
for _, plr in pairs(plrs) do
if plr.Character and plr.Character:FindFirstChild("HumanoidRootPart") and plr.Character:FindFirstChild("Humanoid").Health > 0 then
local hrp = plr.Character.HumanoidRootPart
local distanceBetween = (hrpOfNPC.Position - hrp.Position).Magnitude
if distanceBetween < closestDistance then
closestHRP = hrp
closestDistance = distanceBetween
end
end
end
return closestHRP
end
while wait(pursuitCooldown) do
local targetHRP = getClosestPlayer()
if targetHRP and (hrpOfNPC.Position - targetHRP.Position).Magnitude <= maxDistance then
npc.Humanoid:MoveTo(targetHRP.Position)
if targetHRP.Position.Y > hrpOfNPC.Position.Y then
local raycast = workspace:Raycast(hrpOfNPC.Position, Vector3.new(hrpOfNPC.Position.X, hrpOfNPC.Position.Y + 3, hrpOfNPC.Position.Z), RP)
print(raycast.Instance)
if not raycast.Instance then
npc:MoveTo(hrpOfNPC.Position.X, hrpOfNPC.Position.Y + 1, hrpOfNPC.Position.Z)
end
end
if targetHRP.Position.Y < hrpOfNPC.Position.Y then
local raycast = workspace:Raycast(hrpOfNPC.Position, Vector3.new(hrpOfNPC.Position.X, hrpOfNPC.Position.Y - 3, hrpOfNPC.Position.Z), RP)
print(raycast.Instance)
if not raycast.Instance then
npc:MoveTo(hrpOfNPC.Position.X, hrpOfNPC.Position.Y - 1, hrpOfNPC.Position.Z)
end
end
if targetHRP.Position.Z > hrpOfNPC.Position.Y then
local raycast = workspace:Raycast(hrpOfNPC.Position, Vector3.new(hrpOfNPC.Position.X, hrpOfNPC.Position.Y, hrpOfNPC.Position.Z + 3), RP)
print(raycast.Instance)
if not raycast.Instance then
npc:MoveTo(hrpOfNPC.Position.X, hrpOfNPC.Position.Y, hrpOfNPC.Position.Z + 1)
end
end
if targetHRP.Position.Z < hrpOfNPC.Position.Y then
local raycast = workspace:Raycast(hrpOfNPC.Position, Vector3.new(hrpOfNPC.Position.X, hrpOfNPC.Position.Y, hrpOfNPC.Position.Z - 3), RP)
print(raycast.Instance)
if not raycast.Instance then
npc:MoveTo(hrpOfNPC.Position.X, hrpOfNPC.Position.Y ,hrpOfNPC.Position.Z - 1)
end
end
end
end
humanoid.HealthChanged:Connect(function()
if humanoid.Health <= 0 then
wait(3)
npc:Destroy()
end
end)