i’ve been making a gun using raycasting, except whenever i shoot behind or to my sides the raycast hits the player or the gun, and i’ve tried to exclude the player and gun from raycast collisions but it doesn’t work and im not really experienced with raycasting (i struggle to understand the doc about it, or other tutorials)
code (on a clientscript parented under the tool instance)
local player = game.Players.LocalPlayer
local holdAnim = script.Parent:WaitForChild("holding")
local shoot = script.Parent:WaitForChild("shoot")
local track
local pew
local debounce = false
local active = false
local mouse = player:GetMouse()
local mousepos
local raycast
script.Parent.Equipped:Connect(function ()
track = player.Character.Humanoid:LoadAnimation(holdAnim)
pew = player.Character.Humanoid:LoadAnimation(shoot)
track:play()
end)
script.Parent.Unequipped:Connect(function ()
track:stop()
pew:stop()
active=false
task.wait(0.5)
track:stop()
end)
script.Parent.Activated:Connect(function ()
if active == false then
active = true
end
while active == true do
if debounce==false then
debounce=true
local thingmagigs = {} --(this is where i try to exclude the gun and player by getting a table of the instances under the player's character)
for _,v in pairs(player.Character:GetChildren()) do
if v:IsA("BasePart") then
table.insert(thingmagigs, v)
end
end
local origin = script.Parent.Barrel.bulletspawn.WorldPosition
mousepos= mouse.Hit.Position
local params = RaycastParams.new() -- and this is where i try to insert the table into the exclusion list
params.FilterType = Enum.RaycastFilterType.Exclude
params:AddToFilter(thingmagigs)
local minspread = -1000
local maxspread = 1000
mousepos = Vector3.new(mousepos.X-(math.random(minspread,maxspread)/100),mousepos.Y-(math.random(minspread,maxspread)/100),mousepos.Z-(math.random(minspread,maxspread)/100))
raycast = workspace:Raycast(origin, (mousepos-origin).Unit * 1000 ,params)
if Ray then
print(raycast.Position)
print(raycast.Instance)
print(raycast.Material)
local newhit = Instance.new("Part")
newhit.Parent = workspace
newhit.Position = raycast.Position
newhit.Size = Vector3.new(0.1,0.1,0.1)
newhit.Anchored = true
newhit.Material = Enum.Material.Neon
local newattach = Instance.new("Attachment")
newattach.Parent = workspace.Baseplate
newattach.WorldPosition = raycast.Position
script.Parent.Barrel.bulletspawn.bullettrail.Attachment1 = newattach
end
task.wait(0.01)
debounce=false
elseif debounce==true then
task.wait(0.1)
end
end
end)
script.Parent.Deactivated:Connect(function ()
active=false
end)
i didn’t include the server-side script that spawns the bullet-beam because the raycasting is done only on the client
does anyone have a simple way (since im kind of a moron) to exclude the gun and player from colliding with the raycast?
for _,v in pairs(player.Character:GetChildren()) do
if v:IsA("BasePart") then
table.insert(thingmagigs, v)
end
end
--// and
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params:AddToFilter(thingmagigs)
I would remove the loop and rather than doing AddToFilter, just do params.FilterDescendantsInstances:
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {player.Character} --// should ignore whatever is inside of the character
alternatively, you can
add all parts the characters consist of to a specific CollisionGroup, such as ‘cgroup_01’
then use params.CollisionGroup = ‘cgroup_01’ instead
local plr_svc = game:GetService('Players')
local repl = game:GetService('ReplicatedStorage')
local Dash = require(repl.Dash)
plr_svc.PlayerAdded:Connect(function(plr)
setup_character(plr)
end)
local function setup_character(plr)
local ch = plr.Character or plr.CharacterAppearanceLoaded:Wait()
Dash.forEach(ch:GetChildren(), function(item, key)
if item:IsA('Part') or item:IsA('BasePart') or item:IsA('MeshPart') then
item.CollisionGroup = 'cgroup_01'
end
end)
end
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.CollisionGroup = 'cgroup_01'
(notice this code snippet is using an (official) general purpose library Dash)
notice FilterType and CollisionGroup are mutually exclusive; you cannot use both filters in the same call; if both are set, the behavior is undefined, it seems
GetDescendants works out well for this..
You had a few other issues there also.
-- StarterPack.GunName.LocalScript
local player = game.Players.LocalPlayer
local holdAnim = script.Parent:WaitForChild("holding")
local shoot = script.Parent:WaitForChild("shoot")
local track
local pew
local debounce = false
local active = false
local mouse = player:GetMouse()
local mousepos
local raycast
script.Parent.Equipped:Connect(function()
track = player.Character.Humanoid:LoadAnimation(holdAnim)
pew = player.Character.Humanoid:LoadAnimation(shoot)
track:play()
end)
script.Parent.Unequipped:Connect(function()
track:stop()
pew:stop()
active = false
task.wait(0.5)
track:stop()
end)
script.Parent.Activated:Connect(function()
if active == false then
active = true
end
while active == true do
if debounce == false then
debounce = true
local exclusions = {}
table.insert(exclusions, player.Character)
for _, v in pairs(player.Character:GetDescendants()) do
if v:IsA("BasePart") then
table.insert(exclusions, v)
end
end
local origin = script.Parent.Barrel.bulletspawn.WorldPosition
mousepos = mouse.Hit.Position
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params:AddToFilter(exclusions)
local minspread = -1000
local maxspread = 1000
mousepos = Vector3.new(
mousepos.X - (math.random(minspread, maxspread) / 100),
mousepos.Y - (math.random(minspread, maxspread) / 100),
mousepos.Z - (math.random(minspread, maxspread) / 100)
)
raycast = workspace:Raycast(origin, (mousepos - origin).Unit * 1000, params)
if raycast then
print(raycast.Position)
print(raycast.Instance)
print(raycast.Material)
local newhit = Instance.new("Part")
newhit.Parent = workspace
newhit.Position = raycast.Position
newhit.Size = Vector3.new(0.1, 0.1, 0.1)
newhit.Anchored = true
newhit.Material = Enum.Material.Neon
local newattach = Instance.new("Attachment")
newattach.Parent = workspace.Baseplate
newattach.WorldPosition = raycast.Position
script.Parent.Barrel.bulletspawn.bullettrail.Attachment1 = newattach
end
task.wait(0.01)
debounce = false
elseif debounce == true then
task.wait(0.1)
end
end
end)
script.Parent.Deactivated:Connect(function()
active = false
end)
params:AddToFilter( player.Character ) is all that’s needed, because the FilterDescendantsInstances argument to AddToFilter is an instance or array of instances whose descendants will be included or excluded from the raycast results (based on filterType). Recursively adding all descendant parts is already done on the C++ side, if you do it redundantly in Lua all you do is make your code slower without changing the result.
The gun needs to be part of the filter also .. GetDescendants makes sure everything is accounted for.
“if you do it redundantly in Lua all you do is make your code slower without changing the result.”
For a very small moment, hardly worth considering, and you make sure in one call you got it all.
If the gun is a Tool, and parented inside the character Model as is the convention, it’s already accounted for with params:AddToFilter( player.Character ). If there are models external to the character, you would add those models to the filter also. My point was that there is no scenario where you need to iterate with GetDescendants() and put all descendants into the filter list; the list is supposed to be just the unique set of top-level instances whose descendants you want to filter.
Looping over and adding all the descendants of a character completely unnecessarily is not “good insurance” it’s just lost performance on both the Lua side and because it forces the C++ raycast call to consider all the instances multiple times, even if just to trivially ignore the duplicates. Why not just do it the right way and have cleaner code and better performance?
You’re right.. However I have made guns that this was actually the way to do it as it didn’t work the other way.. I’m just fishing here. The thing is, if that did work out you would know right where the problem is.
There were other things too. Here is a rewrite doing it as you’re saying..
-- StarterPack.GunName.LocalScript
local player = game.Players.LocalPlayer
local holdAnim = script.Parent:WaitForChild("holding")
local shoot = script.Parent:WaitForChild("shoot")
local track
local pew
local debounce = false
local active = false
local mouse = player:GetMouse()
local mousepos
local raycast
script.Parent.Equipped:Connect(function()
track = player.Character.Humanoid:LoadAnimation(holdAnim)
pew = player.Character.Humanoid:LoadAnimation(shoot)
track:play()
end)
script.Parent.Unequipped:Connect(function()
track:stop()
pew:stop()
active = false
task.wait(0.5)
track:stop()
end)
script.Parent.Activated:Connect(function()
if active == false then
active = true
end
while active == true do
if debounce == false then
debounce = true
local origin = script.Parent.Barrel.bulletspawn.WorldPosition
mousepos = mouse.Hit.Position
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params:AddToFilter(player.Character)
local minspread = -1000
local maxspread = 1000
mousepos = Vector3.new(
mousepos.X - (math.random(minspread, maxspread) / 100),
mousepos.Y - (math.random(minspread, maxspread) / 100),
mousepos.Z - (math.random(minspread, maxspread) / 100)
)
raycast = workspace:Raycast(origin, (mousepos - origin).Unit * 1000, params)
if raycast then
print(raycast.Position)
print(raycast.Instance)
print(raycast.Material)
local newhit = Instance.new("Part")
newhit.Parent = workspace
newhit.Position = raycast.Position
newhit.Size = Vector3.new(0.1, 0.1, 0.1)
newhit.Anchored = true
newhit.Material = Enum.Material.Neon
local newattach = Instance.new("Attachment")
newattach.Parent = workspace.Baseplate
newattach.WorldPosition = raycast.Position
script.Parent.Barrel.bulletspawn.bullettrail.Attachment1 = newattach
end
task.wait(0.01)
debounce = false
elseif debounce == true then
task.wait(0.1)
end
end
end)
script.Parent.Deactivated:Connect(function()
active = false
end)