Raycasting Filter Problem

Could someone please explain why this code does not work? It is supposed to only explode when touching anything but the player and ball but when touching the player it explodes.




local rep = game:GetService("ReplicatedStorage")

local ball = rep.Ball
local gravity = -100 -- negative numbers


local fastcast = require(rep.Dependencies.FastCastRedux)

local caster = fastcast.new()
local behavior = fastcast.newBehavior()


local tool = script.Parent




local remoteEvent = game.ReplicatedStorage.Throw
toolball = nil
player = nil



remoteEvent.OnServerEvent:Connect(function(plr, mousePos, plrStrength)

	 player = plr.Character
	toolball = plr.Character.Dodgeball
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Exclude
	params.FilterDescendantsInstances = {toolball, player}
	behavior.RaycastParams = params
	behavior.Acceleration = Vector3.new(0, gravity, 0)
	behavior.AutoIgnoreContainer = true
	behavior.CosmeticBulletTemplate = ball
	behavior.CosmeticBulletContainer = script.Parent.Parent
	local speed = plrStrength
	local pos = toolball.Handle.CFrame * CFrame.new(0,0,-50)
	local direction = (mousePos - toolball.Handle.Position).Unit
	
	caster:Fire(toolball.Handle.Attachment.WorldPosition,direction, speed, behavior)		
return toolball
end)




caster.LengthChanged:Connect(function(ActiveCast, lastPoint, rayDir, displacement, segmentVelocity,  cosmeticBulletObject)
	
	local newPos = lastPoint + (rayDir * displacement)
	cosmeticBulletObject.Position = newPos
	
end)

caster.RayHit:Connect(function(ActiveCast, RaycastResult,  segmentVelocity,  cosmeticBulletObject)


	local  explosion = game.ReplicatedStorage.Explosion:Clone()
	explosion.Size = Vector3.new(20, 20, 20)
	explosion.Position = RaycastResult.Position
	explosion.Parent = workspace

	game:GetService("Debris"):AddItem(explosion, 0.2)
	cosmeticBulletObject.Parent = game.ReplicatedStorage
	end)



I really dont understand why FilterDescendantsInstances isn’t working.

The OnServerEvent callback assumes that the player has a character loaded-in at the time it’s fired. What if plr.Character is nil, though? The character’s body parts won’t be filtered.

Maybe try

player = plr.Character or plr.CharacterAdded:Wait()

Also, I’d recommend you call this variable character instead. You don’t wanna confuse plr and player, trust me :pray:.

2 Likes

I’ve found a temporary fix. Changing the CosmeticBulletContainer works but when spawning the explosion it will spawn an explosion near the player killing them.

I saw the post before you deleted it and that fixed the problem

Huh. I thought maybe that the problem was the explosion destroying the joints of the player, without exception. (see the “Explosion effects” section in here)

The explosion isn’t the problem its just that when throwing I dont want the ball to detonate if it touches the player just the bots, walls, and floor.

What if the player launches the ball at something next to him, like a wall? Wouldn’t the explosion ON THE WALL kill the caster himself? Unless this is intended behavior.

I already have separate code in place for that. I won’t be using the default explosions because they aren’t able to change in size visually. I only used Instance.new(“Explosion”) for faster testing.

1 Like

I actually do still have the problem

Its not putting the player in the FilterDescendants

Ok no actually it is but it is spawning an explosion at the player position too

I think you meant to do

...
caster:Fire(
     toolball.Handle.Attachment.WorldPosition,
     direction * 50, 
     speed, 
     behavior
)	
...

, though I’m not sure.

Not really but this is the code that is spawning multiple explosions




local rep = game:GetService("ReplicatedStorage")

local ball = rep.Ball
local gravity = -100 -- negative numbers


local fastcast = require(rep.Dependencies.FastCastRedux)

local caster = fastcast.new()
local behavior = fastcast.newBehavior()


local tool = script.Parent




local remoteEvent = game.ReplicatedStorage.Throw
toolball = nil
characther = nil

local params = RaycastParams.new()



remoteEvent.OnServerEvent:Connect(function(plr, mousePos, plrStrength)

	characther  = plr.Character or plr.CharacterAdded:Wait()
	toolball = plr.Character.Dodgeball
	
	print(characther)
	
	params.FilterType = Enum.RaycastFilterType.Exclude
	params.FilterDescendantsInstances = {toolball, characther}
	behavior.RaycastParams = params
	behavior.Acceleration = Vector3.new(0, gravity, 0)
	behavior.AutoIgnoreContainer = true
	behavior.CosmeticBulletTemplate = ball
	behavior.CosmeticBulletContainer = characther
	local speed = plrStrength
	local pos = toolball.Handle.CFrame * CFrame.new(0,0,-50)
	local direction = (mousePos - toolball.Handle.Position).Unit
	
	
	caster:Fire(toolball.Handle.Attachment.WorldPosition,direction, speed, behavior)		
end)




caster.LengthChanged:Connect(function(ActiveCast, lastPoint, rayDir, displacement, segmentVelocity,  cosmeticBulletObject)
	
	local newPos = lastPoint + (rayDir * displacement)
	cosmeticBulletObject.Position = newPos
	
end)

caster.RayHit:Connect(function(ActiveCast, RaycastResult,  segmentVelocity,  cosmeticBulletObject)
	
			local explosion = Instance.new("Part")
	explosion.Parent = workspace
	explosion.Position = RaycastResult.Position
	explosion.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			hit.Parent.Humanoid.Health = 0
		end
	end)
	end)