Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would i make my server sided raycasting script ignore players?

I used FindPartOnRayWithIgnoreList for my raycasting script. I want to make it so that it will ignore all players and target only the NPCS i created instead. Problem is, i can't find a way to add a table of all players in the game inside the ignore list. Honestly i'm not even sure if that's possible.

local ball = script.Parent

function findTarget()
	local radius = 25
	local target = nil
	
	for i,v in pairs(game.Workspace:GetChildren()) do
		local root = v:FindFirstChild('HumanoidRootPart')
		local hum = v:FindFirstChild('Humanoid')
		
			
		if root and hum then
			if (ball.Position - root.Position).magnitude <= radius and hum.Health > 0 then
				ball.CFrame = CFrame.new(ball.Position, root.Position) 
				
				local ray = Ray.new(ball.Position,(root.Position - ball.Position).Unit * 500)
				local hit,position = game.Workspace:FindPartOnRayWithIgnoreList(ray,{})
				if hit then
					if hit:IsDescendantOf(root.Parent) then 
						radius = (ball.Position - root.Position).magnitude 
						target = root
					else
						print(hit.Name)
					end
				end
			end
		end
	end
		
	return target
end

5 comments

1

Have you tried adding a BoolValue to the NPC's Named "IsNPC" and then searching for players with a Humanoid but without a IsNPC tag?

0

I did the exact opposite, i inserted a tag inside the players to differentiate them from my NPCS, but i soon figured there was no way to get the character model.

1

Adding onto what boomthefist said, you could add any value inside of the NPC and check if they have the tag, and if not then don't do anything, but if they do have it, then do what you are trying to do.

0

You could do a for loop of every player and get their character and it to the ignore list.

0

I'll try it out, thanks! I didn't think of that lol.

1 answer

✓ Accepted answer

There's 2 ways that I know of, one of them @boomthefist said, and another way is a for loop.


First Method - Value

  • Create any sort of value inside of the NPC's with a distinguishable name that a player or character would not have.

  • Check for the value inside of the hit function by doing:

if hit.Parent:FindFirstChild("Value You Are Looking For") then
	--// Rest Of Code
end

Second Method - For Loop

local ignoreList = {}

for _,player in pairs(game:GetService("Players"):GetPlayers()) do
	local character = player.Character or player.CharacterAdded:Wait()
	table.insert(ignoreList,character)
end

local hit,position = game.Workspace:FindPartOnRayWithIgnoreList(ray,ignoreList)

This will loop through all the players, get their characters, and insert them into a table called ignoreList.


Hope this helped!

2 comments

0

Ty!

0

Np! Glad to help!

This discussion is preserved as it was. New answers, comments, and votes are closed.