Raycasting going everywhere but where it's supposed to go

For some reason, my raycast is either not going towards the player or going in a completely different direction and I have absolutely no clue on why it is happening. Any help?

	for i,v in pairs (Players_In_Range) do
		local Distance = (Throwable_Clone.Position - v.Character.HumanoidRootPart.Position).Magnitude 
		
		if Distance <= BlastRadius then
			local params = RaycastParams.new()
			params.FilterType = Enum.RaycastFilterType.Blacklist
			params.FilterDescendantsInstances = {Throwable_Clone}
				
			local raycast = workspace:Raycast(Throwable_Clone.Position,v.Character.HumanoidRootPart.Position,params)
			print(raycast)
			
			if raycast.Instance:FindFirstChild("Humanoid") then
				if raycast.Instance:FindFirstChild("Humanoid").Health <= 0 then
				else
					raycast.Instance:FindFirstChild("Humanoid").Health = raycast.Instance:FindFirstChild("Humanoid").Health - (Damage - (Damage * (Distance/BlastRadius)))
				end
			end
		end
	end

Pretty much it loops through a table of players and takes the nade’s position the shoots a ray at the said player. It is accurately grabbing both the nade and the player’s position since I’ve already checked those two with print statements. I’m just confused on why the ray isn’t casting correctly.

2 Likes

Did you forget to reference your Distance variable for the second parameter of your raycast?

              local raycast = workspace:Raycast(Throwable_Clone.Position,Distance,params)
1 Like

That wouldn’t be a vector3 though. That’s just grabbing the magnitude which is just a double.

2 Likes

Whoops

Maybe you could check if there’s a valid raycast first? If there is, you could print the results

			if raycast then
                print("Object/terrain hit:", raycastResult.Instance:GetFullName())
            	print("Hit position:", raycastResult.Position)
            	print("Surface normal at the point of intersection:", raycastResult.Normal)
            	print("Material hit:", raycastResult.Material.Name)

				if raycast.Instance:FindFirstChild("Humanoid") and raycast.Instance:FindFirstChild("Humanoid").Health <= 0 then
				else
					raycast.Instance:FindFirstChild("Humanoid") and raycast.Instance:FindFirstChild("Humanoid").Health = raycast.Instance:FindFirstChild("Humanoid").Health - (Damage - (Damage * (Distance/BlastRadius)))
				end

            else
                print("No result found")
			end
1 Like

I did. The raycast goes on the Z axis and hits random parts rather than towards the player.

The second parameter for raycasting is for direction, try doing

local raycast = workspace:Raycast(Throwable_Clone.Position,v.Character.HumanoidRootPart.Position-Throwable_Clone.Position,params)

from what I can see your second parameter was an end position so this should work.

1 Like

this should fix it

local BlastRadius = 40
local Damage = 150
local Throwable_Clone = workspace.grrr

local Players_In_Range = {}

local function Find_Players(nade)
	table.clear(Players_In_Range)
	for i,v in pairs (game.Players:GetChildren())do
		if v.Character then
			if v.Character:WaitForChild("HumanoidRootPart") then
				if v.Character:WaitForChild("Humanoid").Health > 0 then
					if (v.Character.HumanoidRootPart.Position - nade.Position).Magnitude <= BlastRadius then
						local Distance = (v.Character.HumanoidRootPart.Position - nade.Position).Magnitude
						table.insert(Players_In_Range,{plr=v,dis=Distance})
						print(v.Name)
						return Players_In_Range 
					end
				end
			end
		end
	end
end
Find_Players(Throwable_Clone)
wait()
local function ONLYPlayers()
	local t = {Throwable_Clone}
	for i ,v in pairs(game.Players:GetPlayers()) do
		if v.Character then
			for i, ob in pairs(v.Character:GetChildren()) do
				if ob:IsA("Accessory") or ob:IsA("Hat") then
					table.insert(t,ob)
				end
			end
		end
	end
	for i ,v in pairs(workspace:GetDescendants()) do
		if not v:IsA("BasePart") then continue end
		if v.Transparency==1 then
			table.insert(t,v)
		end
	end
	return t
end


for i,v in pairs (Players_In_Range) do
	local plr = v.plr
	local dis = v.dis
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Blacklist
	params.FilterDescendantsInstances = ONLYPlayers()
	local raycast = workspace:Raycast(Throwable_Clone.Position,(plr.Character.HumanoidRootPart.CFrame.p-Throwable_Clone.CFrame.p).Unit*9000,params)
	if raycast.Instance.Parent:FindFirstChild("Humanoid") then
		if raycast.Instance.Parent:FindFirstChild("Humanoid").Health > 0 then
			print( Damage - (Damage * (dis/BlastRadius)) )
			raycast.Instance.Parent:FindFirstChild("Humanoid").Health -=(Damage - (Damage * (dis/BlastRadius)))
		end
	end
end
1 Like