Why isn't Raycasting working?

Hey

So what I am trying to do is make my very own custom NPC movement script for my enemy, everything is going alright, but its just an issue on the RayCasting side of things.

What I am trying to do is find if there is a HumanoidRootPart facing towards the NPC, and if so, then it should print yes. If there is not a HumanoidRootPart facing towards the NPC, then it should print no.

For some reason, the RayCast only seems to print yes but not no when the player is facing the opposite direction of the enemy.

Here is the script for the raycast: (Module)

local module = {}

function module.cast (character, self)
	self = module
	self.chr = character
	self.hrp = self.chr:WaitForChild('HumanoidRootPart')
	self.rs = game:GetService('RunService')
	
	self.raycastParams = RaycastParams.new()
	self.raycastParams.FilterDescendantsInstances = {self.hrp}
	self.raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	self.raycastResult = workspace:Raycast(self.hrp.Position, self.hrp.CFrame.RightVector * -100, self.raycastParams)
	self.rs.Heartbeat:Connect(function(dt)
		if self.raycastResult then
			self.hitPart = self.raycastResult.Instance
			if self.hitPart.Parent:FindFirstChild('HumanoidRootPart') then
				print('yes')
			else
				print('no')
			end
		end
	end)
	
	return self
end

return module

Let me know if you find something wrong, or find a solution. Thank you
(this is just a test enemy btw lol)

1 Like

The issue is that you only raycast once, meaning if it detected something, it would only print yes. Try putting the entire raycast system inside of the Heartbeat event, and it should start working.

local module = {}

function module.cast (character, self)
	self = module
	self.chr = character
	self.hrp = self.chr:WaitForChild('HumanoidRootPart')
	self.rs = game:GetService('RunService')
	
	self.raycastParams = RaycastParams.new()
	self.raycastParams.FilterDescendantsInstances = {self.hrp}
	self.raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	self.rs.Heartbeat:Connect(function(dt)
        self.raycastResult = workspace:Raycast(self.hrp.Position, self.hrp.CFrame.RightVector * -100, self.raycastParams)
		if self.raycastResult then
			self.hitPart = self.raycastResult.Instance
			if self.hitPart.Parent:FindFirstChild('HumanoidRootPart') then
				print('yes')
			else
				print('no')
			end
		end
	end)
	
	return self
end

return module

also im not sure if its supposed to be rightvector or lookvector. but yeah that fixed that. rightvector means right of the character (FROM WHERE ITS FACING). and look vector is (WHERE THE CHARACTER IS LOOKING LIKE INFRONT)

its still acting weird. if you want the place file then lmk.
i tried your script, rearranged it a bit, and now its just printing no.

tried this too, its just printing no now.

ykw ima just reteach myself about raycasting :sob:
(the humanoidrootpart wasn’t being detected, so since this project is 2d, its gonna detect the right arm instead lol)

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