Bullet Damage Detection

So I just made a gun that projects bullets using RayCasting.

[EDIT]
I’ve decided to take my code apart and show the first position of the bullet, and then a second position marked by two red parts. I tried to find the parts on the ray between these two, and for the majority of the time, nothing printed, and I’m wondering if I’m using the functions incorrectly.

Here is my code:

mouse.Button1Down:Connect(function()
local ray = Ray.new(gun.Nuzzle.CFrame.p, (mouse.Hit.p - gun.Handle.CFrame.p).unit * 5)
local part, position = workspace:FindPartOnRay(ray, char, false, true)

local beam = Instance.new("Part", workspace)
beam.BrickColor = partColor
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false

local distance = (gun.Nuzzle.CFrame.p - position).magnitude
beam.Size = Vector3.new(0.1, 0.1, distance)
beam.CFrame = CFrame.new(gun.Nuzzle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

if part then
	game:GetService("Debris"):AddItem(beam, .05)
	local humanoid = part.Parent:FindFirstChild("Humanoid")
	if not humanoid then
		humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
	end
	if humanoid then
		gunAction.Damage:FireServer(humanoid)
	end
end

game:GetService("RunService").Heartbeat:wait()
if beam == nil then return end

local mark1 = game.ReplicatedStorage.mark:Clone()
mark1.Parent = game.Workspace
mark1.CFrame = beam.CFrame*CFrame.new(0, 0, -2.5)

local mark2 = game.ReplicatedStorage.mark:Clone()
mark2.Parent = game.Workspace
mark2.CFrame = beam.CFrame*CFrame.new(0, 0, -7.5)

local rayo = Ray.new(mark1.CFrame.p, mark2.CFrame.p)
local parto, positiono = workspace:FindPartOnRay(rayo, char, false, true)

if parto then print(parto.Name.." belongs to "..parto.Parent.Name) end

end)

Any help is appreciated <3

4 Likes

Wrap your projectile movement (RunService.Heartbeat). If you add the projectile to debris service, you can check every step if beam == nil or beam.Parent = nil. If true, call a :Disconnect() on the Heartbeat Event.

For damage detection, raycast from the end of the beam to its current end goal. If magnitude < 1 then, take damage and remove the beam.

I wrote this half asleep so, I could be missing something.

Thank you, I’ll try that next! I’m taking my code apart right now to find out what the problem is :slight_smile: your help is super appreciated

1 Like

Having this in your code would allow exploiters to fire the remote to kill any player they want, I suggest controlling damage via the server. You could use a Module EtiTheSpirit called FastCast to make life easier

1 Like

I have a server function to check this damage. I have looked into FastCast and will do it again, thank you! :slight_smile:

If a ray doesn’t hit any surface, the first value it returns which is supposed to be the object that it hit (in your script it’s called parto) will be nil, which means the if statment after that line, will not proceed, and nothing will be printing. As you show in the video, when you shoot the ray in thin air nothing gets printed, but as soon as you touch the wall all is good.

1 Like

The problem was I was giving two vectors, though there needs to be a direction and magnitude (unit), which is why it wasn’t processing correctly (TypicalType helped me on this, so thank you)

Unfortunately, even when I touch the wall it sometimes won’t even print. from the video, even when I shoot the soldiers no body part returns, but I have solved the problem. Your help is appreciated <3

1 Like