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 make my gun not shoot through walls?

I've made a gun that uses raycasting but the problem is that the gun can shoot through walls. Please help

https://gyazo.com/a3004e6f4ea197fc00a819538879a73c

code:

local function part(hit,pos,target)
if script.Parent.Ammo.Value>0 then
script.Parent.Ammo.Value=script.Parent.Ammo.Value-1

local ray=Ray.new(script.Parent.Handle.CFrame.p,(script.Parent.Handle.CFrame.p-pos.p).Unit*100)

	local part,hitpos=workspace:FindPartOnRay(ray,script.Parent,false,true)
	local part=Instance.new("Part",workspace.Bullets)
	local mag=(pos.p-script.Parent.Handle.Position).Magnitude
	part.CFrame=CFrame.new(script.Parent.Handle.Position,pos.p)*CFrame.new(0,0,-	mag/2)
	part.Size=Vector3.new(.5,.5,mag)
	part.Anchored=true
	part.Locked=true
	part.FormFactor=("Custom")
	part.CanCollide=false
	part.Transparency=.5
	part.Material=("Neon")
	game.Debris:AddItem(part,.5)

if target.Parent:FindFirstChild("Humanoid")then
	target.Parent.Humanoid:TakeDamage(10)
end
print(target)
end
end

script.Parent.RemoteEvent.OnServerEvent:Connect(part)

7 comments

1

PLEASE, indent your code.

0

I did try that tutorial but I still couldn't find a reason on why my gun was shooting through walls.

0

What is the "pos" variable and what is its value?

0

i'm guessing that "Target" is a mouse hit target? and "pos

0

make it cancollided

0

dude, its obvious that that code was taken off of the internet, my suggestion would be to learn basic lua before moving onto other topics such as raycasting. If you dont know basic part propeties, i would suggest learning before everything else. Sorry if this comment seems rude or insulting, my intent is to try and get him to have a better grasp on what hes doing. Answer: part.CanCollide=true

1 answer

make it cancollided
part.CanCollide=true

also read my comment

upvote please

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