Fixing Bullet Penetration

I’m working on a 3rd person gun that shoots at players when clicked by the mouse. (No camera lock and 3rd person.) Players can currently click on other players and shoot them even if they are behind a wall. I’ve tried to work with Raycasting, but I can’t seem to work it out. Are there alternative methods I can use for this? If not, how could I go about implementing Raycasting into my gun?

Revolver > LocalScript (Fire)

--variables
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local debounce = false
--

script.Parent.Activated:Connect(function()
	local target = mouse.Target
	if target.Parent:FindFirstChild("Humanoid") and debounce == false then
		script.Parent.RemoteEvent:FireServer(target.Parent)
		debounce = true
		wait (1.5)
		debounce = false
	end
end)

Revolver > Script (damage)

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player, target)
	 target.Humanoid:TakeDamage(20)
end)```
1 Like

First of all, you should use Raycasting on the server (to prevent exploiting and fix being able to kill things through walls) and secondly, To do bullet penetration, just check if the surface the ray hit was small enough, then fire a second ray on the other side of the wall.

2 Likes

I’ve rewrote the script, and it now successfully works with ray casting! Players cannot shoot through walls, but if their gun tool is clipping through the wall, it will let them do so. Any ideas on how to fix this?

Gun Main Script:

--variables
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local debounce = false
local Tool = script.Parent
local User
--
Tool.Equipped:connect(function(mouse)
	User = Tool.Parent
	mouse.Button1Down:connect(function()
		local Ray = Ray.new(Tool.Handle.CFrame.p,(mouse.Hit.p-Tool.Handle.CFrame.p).unit*300)
		local Hit,Position = game.Workspace:FindPartOnRay(Ray,User)
		if Hit and debounce == false then
			if Hit.Parent:FindFirstChild("Humanoid") then
				Hit.Parent.Humanoid:TakeDamage(30)
				debounce = true
				wait(1.5)
				debounce = false
			end
		end
	end)
end)

Anything helps! Thank you so much!

So basically, you can mark certain walls as penetrable, which in that case you can just ignore all of them using RayCastParams, also, use the newer workspace:Raycast() system as Workspace:FindPartOnRay() is deprecated. You could also loop through all of the thin parts in workspace and then filter those out at the start of the game.

Thanks! I was using an outdated raycasting tutorial, ill experiment with this.

You could also measure the thickness of the part you just hit, and if it is thin enough you can refire the ray but with that specific part filtered out.

I’m not trying to make a specific wall penetrable; I’m trying to make all walls not penetrable

oh, I assumed you were trying to implement bullet penetration.

1 Like

Wait, are you doing the raycast on the client?

1 Like

Not completely sure, newbie scripter.

p.s. do I use vector3 to create a ray?

its a local script so i think so

This is a bad scripting practice, consider moving it to the server to prevent a major exploit vulnerability in your game. Since the player can just give whatever player they want to kill and the server will kill them without question.

1 Like

Also, you can do :TakeDamage() on the client, as it will not appear for other players.

1 Like

:TakeDamage() will not appear?

So basically, if you do not understand FilteringEnabled and how multiplayer games work, what you are actually seeing when you play a game is called a “client” and the server is the middleman. Everything that happens on the server will also happen on a client, but nothing that happens on a client is replicated to the server. So, since this is a local script, only the person who fired the gun is actually able to see the damage, and the person will not die when their health reaches zero because they didn’t actually die on their client. A good article on this is from the unofficial roblox wiki.

1 Like

Got it. Should I be firing an event that tells the server to cause damage?

No, not at all, all you should be giving the server is where you would like to fire the ray, nothing else.

1 Like

Alright, so I’ll try to figure out how to move the damage to the server. Thank you for the help!

Yeah, also try and move the raycasting to the server aswell, so that exploiters can’t ruin your game.

1 Like

This advice is the wrong approach. Raycasting on the server will result in latency. You need to raycast on the client to minimize latency and then validate that on the server. The most practical way to do a gun system is with raycast.

2 Likes