I was doing an npc that detects a player in front and increases their npc speed, but it doesn’t seem to work, are there possible reasons?
local NPCModel = script.parent
local endPosition = script.Parent:WaitForChild("Endpos")
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {NPCModel, endPosition, script.Parent.Head}
raycastParams.IgnoreWater = true
-- Cast the ray
while wait(0.1) do
for i,v in pairs(game:GetService("Players"):GetPlayers()) do
local raycastResult = workspace:Raycast(script.Parent.Head.Position, endPosition.Position, raycastParams)
-- Interpret the result
if raycastResult then
print("Got Player")
repeat wait(0.15) NPCModel.Humanoid.WalkSpeed += 1 until NPCModel.Humanoid.WalkSpeed == 125
print("Hit position:", raycastResult.Position)
else
print("Nothing was hit!")
end
end
end
What is “Endpos”? workspace:Raycast() recieves an origin and a direction, not an ending position. If I would have to detect something right in front of the character with Raycast I would use HumanoidRootPart and its CFrame.
local Result = workspace:Raycast(HRP.Position, HRP.CFrame.LookVector, Params)
So, if i have NPC Lookvector calculated, how i would detect the player in front of it?, i mean, how i can set the player character, i noticed that i , v in pairs(game:GetService("Players"):GetPlayers()) doesn’t works
You should use Unit vector as Sarchyx says, but I never had any luck using a variable for the unit, myself… I had to use a constant: 7. Probably something stupid i was doing…?
And don’t loop though Players. Just cast one ray, an check what it hit…
Here is a model, which pathfinds using rays, made b4 Pathfinding existed…
Canabalize it…