Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I check if my gun is colliding through a wall?

I'm currently making an fps and I am stumped on how to proceed with preventing shooting through walls (sticking your gun through a wall then firing).

Currently I'm considering giving the gun a hitbox and preventing it from firing if the hitbox hits a wall (edit: INSIDE a wall), however, I have no idea how to do this - I've experimented with touched and I don't know how to properly use it to do this. Any help is greatly appreciated! Thanks.

4 comments

0

You could try casting a ray from the tip of the gun to your player's head, if it can't hit, it would mean they're shooting through a wall

0

I don't mean bullet collision detection, I mean actual weapon detection - the weapon itself has a hitbox and can't fire if it is stuck through a wall to prevent shooting through walls, where the tip would be on the other side of a wall.

1

yeah that's what i'm saying, you could try casting a a ray from the tip of the gun to your head. if there's something in the way, they're probably trying to shoot through a wall.

0

Ah, I see. Sorry, I misread your previous answer. I'll try this now.

1 answer

I was afraid of someone doing this while making my gun for a murder game,so i decided to make a ray that comes from the head of the player,instead of the gun.

--Local Script

tool=script.Parent
clickEvent=tool.ClickEvent

tool.Equipped:connect(function(mouse)
	mouse.Button1Down:connect(function()
		clickEvent:FireServer(mouse.Hit.p)
	end)
end)

--Server script

tool=script.Parent
clickEvent=tool.ClickEvent

tool.Equipped:connect(function()
	local player=tool.Parent
	clickConnection=clickEvent.OnServerEvent:connect(function(hit)
		local ray=Ray.new(player.Head,hit)
		local part=workspace:FindPartOnRay(ray,player)
		if part.Parent:FindFirstChild('Humanoid') then
			part.Parent.Humanoid:TakeDamage(30)
		end
	end)
end)
		

Hope this helped ya :D

This discussion is preserved as it was. New answers, comments, and votes are closed.