Raycast not working

Hello! I am making an advanced ai right now, and my raycast function to check if there is a direct line to the player is not working. The error says, and I quote, “Unable to cast Instance to Vector3”. I do not understand what this means? I mean, at the current moment, I’m following this part off of a tutorial, and the tutorial uses old raycasting. IE: local hit, position stuff like that. I converted it into modern raycasting, but thats not working. Edit: The code in the modern raycast is the exact code as shown in the deprecated one, however this modern version seems to not work.

**Edited code**
	local r_params = RaycastParams.new()
	r_params.FilterType = Enum.RaycastFilterType.Blacklist
	r_params.FilterDescendantsInstances = {rig}

	local raycast_result = workspace:Raycast(human_root, (target.Position - human_root.Position).Unit * 40, r_params)
**Unedited code**
--//Checking the sight of the ai to see if there is a player
local function raycast_sight(target)
	--raycasting
	local r_params = RaycastParams.new()
	r_params.FilterType = Enum.RaycastFilterType.Blacklist
	r_params.FilterDescendantsInstances = {rig}

	local raycast_result = workspace:Raycast(human_root, (target.Position - human_root.Position).Unit * 40, r_params)

	if raycast_result then
		local instance = raycast_result.Instance
		--checking if its a model
		if instance:IsDescendantOf(target.Parent) and math.abs(raycast_result.Position.Y - human_root.Position.Y) < 3 then --if the instance is a similar height to the AI
			print("target_spotted")
			return true
		end
	end
	return false
end

Any help will be appreciated :cowboy_hat_face:

This error is telling you that you’re trying to set an Instance to a position (Vector3) which won’t work because the Instance is well, an instance and a Vector3 isn’t

Which line is erroring? (Send that line)

1 Like

Woah you don’t need to mark me as a solution, at least not yet. I haven’t even solved your problem yet

Nvm

1 Like

I don’t know why I didn’t think of making it hrp.Position :sweat_smile:
You’re a savior!

1 Like

It actually has worked for me!

1 Like

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