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

How To Ignore Parts With Raycast?

Hello! I heavily modified a gun script into a turret script and I have a problem and you might have the solution :D When the weapon is fired, the part uses raycasts and fancy stuff to determine where it goes but when the player clicks in rapid succession, the raycast will catch the previously fired bullet and cause it to not continue into oblivion.

I heard about using :FindPartOnRayWithIgnoreList() to ignore stuff and I have been unable to get it to properly work. How could I make the raycast ignore all other bullets?

Local Script:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local mouse = player:GetMouse()
mouse.TargetFilter = workspace

local difference = 0
local shootevent = game.ReplicatedStorage:WaitForChild("Shoot")
local gun = game.Workspace:WaitForChild("GunPart")

mouse.Button1Down:Connect(function()
	local head = game.Workspace[player.Name].Head.CFrame.lookVector
	local mouse = CFrame.new(game.Workspace[player.Name].Head.Position,mouse.Hit.p).lookVector
	difference = (head - mouse)
	local ray = Ray.new(gun.CFrame.p,(player:GetMouse().Hit.p - gun.CFrame.p).unit*300)
	local part,position = game.Workspace:FindPartOnRay(ray,player.Character,false,true)
	
	--shootevent:FireServer(gun,position,part)
	if difference.magnitude < .2 then
		shootevent:FireServer(gun,position,part)
	end
end)

Server Script:

local serverStorage = game:GetService("ServerStorage")
local replicatedStorage = game:GetService("ReplicatedStorage")

replicatedStorage.Shoot.OnServerEvent:Connect(function(player,tool,position,part)
	
	local distance = (tool.CFrame.p - position).magnitude

	local bullet = game.ReplicatedStorage.Bullet:Clone()
	bullet.Position = tool.Position
	bullet.A1.Position = Vector3.new(0,0,-.2)
	bullet.A0.Position = Vector3.new(0,0,.2)
	bullet.CanCollide = false
	game:GetService("Debris"):AddItem(bullet, 5)
	bullet.Parent = workspace
	
	local ray = Ray.new(bullet.Position, (position - bullet.Position).Unit*4000)
	
	local hit, position = workspace:FindPartOnRay(ray, player.Character)
	local distance = (ray.Origin - position).Magnitude
	local arrival = (distance/500)
	local tween = game:GetService("TweenService"):Create(bullet, TweenInfo.new(arrival, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0), {Position = position})
	tween:Play()
	tween.Completed:Connect(function()
		bullet.Transparency = 1
	end)
end)

1 answer

Oops, I answered this myself but the site recommends I post an answer rather than delete. All I did was replace line 18 in the server script with local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, {player.Character,game.Workspace})

local serverStorage = game:GetService("ServerStorage")
local replicatedStorage = game:GetService("ReplicatedStorage")

replicatedStorage.Shoot.OnServerEvent:Connect(function(player,tool,position,part)
	
	local distance = (tool.CFrame.p - position).magnitude

	local bullet = game.ReplicatedStorage.Bullet:Clone()
	bullet.Position = tool.Position
	bullet.A1.Position = Vector3.new(0,0,-.2)
	bullet.A0.Position = Vector3.new(0,0,.2)
	bullet.CanCollide = false
	game:GetService("Debris"):AddItem(bullet, 5)
	bullet.Parent = workspace
	
	local ray = Ray.new(bullet.Position, (position - bullet.Position).Unit*4000)
	
	local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, {player.Character,game.Workspace})
	local distance = (ray.Origin - position).Magnitude
	local arrival = (distance/500)
	local tween = game:GetService("TweenService"):Create(bullet, TweenInfo.new(arrival, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0), {Position = position})
	tween:Play()
	tween.Completed:Connect(function()
		bullet.Transparency = 1
	end)
end)

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