Raycast isn't detecting things half of the time

I am currently making a proximity text-chat feature.

What i want to happen: Raycast from the player to the other player and see if there is a wall in the way

what happens: one player will return a hit, one won’t

code for raycast:

        local map = {workspace.Map}
		local params = RaycastParams.new()
		
		params.FilterType = Enum.RaycastFilterType.Whitelist
		params.FilterDescendantsInstances = map
		
		local ray = workspace:Raycast(headpos,pos,params)
		
		if ray then
			print('hit something, dampening')
		else
			print('nothing hit')
		end

image

EXAMPLES OF PROBLEM:


image

image

Try replacing your FilterDescendantsInstances to:

params.FilterDescendantsInstances = {workspace.Map}

It accepts arrays, but not just instances.

just tried it, still having the issue, im guessing it has to do with the origin point and destination point, do you think that could be it?

Oops, I just realized the map variable was already a table. The origin and direction is probably the issue then. Keep in mind you said “destination point”, when the parameter is actually a direction. That might be the issue. Could you show the variables for the 2 points?

ah, i thought it was point to point, thats probably the issue then

local headpos = script.Parent.Parent.Parent.Parent.Character.Head.Position

other is from a function with a remote event

game.ReplicatedStorage.Chatted.OnServerEvent:Connect(function(plr,msg,pos)

that’s fire is this:

game.ReplicatedStorage.Chatted:FireServer(script.Parent.Text,game.Players.LocalPlayer.Character.Head.Position)

ill mess with it and see if i can get it to be a direction, give me a bit to read up on it and ill report back

Could I see what the second parameter is the cast function was, the “pos”?

Also, I see some things that aren’t related to the problem that you should change.

  1. Keep remotes in an organized folder in ReplicatedStorage. Your remotes are directly in RS, putting them in a folder is more organized. This avoids having things scattered throughout RS.
  2. Avoid spamming .Parent. Reference to the actual character instead.
  3. You can actually do everything on the client (raycast, npc messages, etc.). This takes some load off the server which will create less lag.
  4. It seems your script is located somewhere inside Workspace inside a character. I wouldn’t do this, as it creates multiple unnecessary scripts. Most of the time you’ll want to put server scripts inside ServerScriptService. This uses one master script instead of multiple duplicated scripts, which again, creates less lag.

These are just some good practices you should get use to, they’ll help you later down the line.

1 Like

pos is the players head from a remote event, and yeah, i just started this project a bit ago and havent done much optimization on anything yet

1 Like

Also, here is a hint to your solution (not really).

local direction = endPos - origin

just realized i got it down, but had not read it right

only 1 client needs to see if it is behind a wall, as another is sending the message

this script worked, but i was only reading the bottom line

what i could see: (bottom line only)
image

what it was printing: (bottom and top lines)
image

thanks, i gotta do some of those things you reccomended

1 Like