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
PLEASE, indent your code.
Heres a raycasting roblox tutorial: https://developer.roblox.com/en-us/articles/Making-a-ray-casting-laser-gun-in-Roblox
I did try that tutorial but I still couldn't find a reason on why my gun was shooting through walls.
What is the "pos" variable and what is its value?
i'm guessing that "Target" is a mouse hit target? and "pos
make it cancollided
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