Ok so I created a gun using workspace:RayCast() and the raycast works but when I try to damage a player when the laser hits them, it doesnt damage the player. I know why though, its because the mouse is on the laser not the player so it wont detect it. I tried to fix by creating a blacklist using Enum.RaycastFilterType.Blacklist so it doesnt detect the laser but It doesnt work, here is my code how could I get around this? (let me know if you need a better explanation)
TLDR: My raycast gun wont deal damage to a player because the mouse is on the laser not the player
event.OnServerEvent:Connect(function(player, toP, fromP)
local direction = (toP - fromP)
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {handle} -- Handle is the handle of the gun
raycastParams.IgnoreWater = false
local result = workspace:Raycast(fromP, direction, raycastParams)
-- Damage
if result then
local hit = result.Instance
print(hit) -- this always prints "Part" which is the laser
if hit.Parent:FindFirstChild("Humanoid") then
if hit and hit.Parent.Humanoid ~= player.Character.Humanoid then
hit.Parent.Humanoid:TakeDamage(damage)
end
elseif hit.Parent.Parent:FindFirstChild("Humanoid") then
if hit and hit.Parent.Parent.Humanoid ~= player.Character.Humanoid then
hit.Parent.Parent.Humanoid:TakeDamage(damage)
end
end
end
local midPoint = fromP + direction /2
local laser = Instance.new("Part")
laser.Parent = game.Workspace
laser.Anchored = true
laser.CanCollide = false
laser.Locked = true
laser.BrickColor = BrickColor.new("Bright yellow")
laser.Material = Enum.Material.Plastic
laser.CFrame = CFrame.new(midPoint, fromP)
laser.Size = Vector3.new(.25, .25, direction.magnitude)
game.Debris:AddItem(laser, 1
end)
The ray probably doesnt intersect the player. Note that the length of the direction vector matters, so when the mouse is on the player the ray just stops when it hits the player, not intersecting it. you should increase the magnitude of direction with something like direction = 500 * (toP - fromP).Unit
which would give the ray a maximum detection distance of 500
Any errors in the output? Try adding a print statement to return the name of whatever the ray-cast is hitting. Proceed to blacklist said thing.
Side note, I noticed at the bottom of the script, you don’t add the closing bracket to the Debris line. Should probably close that up otherwise that will likely error.
To make it more clear, the damage system works by detecting where your mouse is pointing, and if its pointing at a player then it will make them take damage. But even when I aim at the player it still wont damage them because the laser is in the way.
See how the mouse is pointing at the laser not the actual player? I’m looking for a way to ignore the laser so it can detect the player. RaycastFilterType wont work because a new laser is always being created and destroyed.
You can make a folder in workspace specifically for these laser parts and add that to the blacklist for the filter and that will stop the ray cast from hitting the parts.
Sorry for bumping but your solution helps me so much, I made gun beam in workspace folder but not blacklist filtering descendants instances it (the folder) make my shotgun won’t work. Thank you so much!