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

Shooting through walls?

Okey so i made this gun that works fine but the only problem is that the bullets goes through walls and thats kinda annoying

Player = game.Players.LocalPlayer
Gun = script.Parent
firespeed = 0.07
Reloadtime = 1.5
Ammo = 30
spareammo = 300
mouse = Player:GetMouse()
Dmg = math.random(20,25)
enabled = true

Gun.Equipped:connect(function()
	script.Parent.GripForward = Vector3.new(0,-150,1)
end)

function shoot()
	if Ammo > 0 and enabled == true then
		enabled = false
		IsFiring = true
		Firing()
	end
	
end
function Firing()
	repeat
	function Damage(Part)
			if Part.Parent:FindFirstChild("Humanoid") ~= nil and Part.Parent.Name ~= Player.Name and game.Players:GetPlayerFromCharacter(Part.Parent).TeamColor ~= Player.TeamColor then
				Part.Parent.Humanoid.Health = Part.Parent.Humanoid.Health -Dmg
				Bullet:Destroy()
			end
		end
		Ammo = Ammo -1
		enabled = false
		Bullet = Instance.new("Part", workspace)
		Gun.GripPos = Vector3.new(-0.35,1.2,0)
		Bullet.Shape = "Ball"
		Bullet.Size = Vector3.new(0.2,0.2,0.2)
		Bullet.TopSurface = "Smooth"
		Bullet.BottomSurface = "Smooth"
		Bullet.BrickColor = BrickColor.new("Dark stone grey")
		Bullet.CFrame = Gun.Handle.CFrame +Vector3.new(0,0.35,0.4)
		Bullet.CFrame = CFrame.new(Bullet.Position,mouse.hit.p)
		Bullet.CanCollide = true
		BM = Instance.new("SpecialMesh", Bullet)
		BM.MeshType = "Sphere"
		BM.Scale = Vector3.new(0.2,0.2,0.2)
		v = Instance.new("BodyVelocity", Bullet)
		v.velocity = Bullet.CFrame.lookVector *250
		v.maxForce = Vector3.new(math.huge,math.huge,math.huge)
		Sound = Instance.new("Sound", Bullet)
		Sound.SoundId = "http://www.roblox.com/asset/?id=132455948"
		RP = math.random(60,80)
		RP = RP/10
		Sound.Pitch = RP
		RV = math.random(70,90)
		RV = RV/10
		Sound.Volume = RV
		Sound:Play()
		Bullet.Touched:connect(Damage)
		game.Debris:AddItem(Sound, 2)
		wait(firespeed)
		Gun.GripPos = Vector3.new(-0.3,1,0)
		wait()
		enabled = true
	until IsFiring == false or Ammo == 0
end
function stopshooting()
	IsFiring = false
end
function reload(key)
	key:lower()
	if key == "r" then
		game.Players.LocalPlayer.PlayerGui.weapongui.ReloadingLabel.Visible = true
		spareammo = spareammo - Ammo
		print("Reloading")
		IsFiring = false
		wait(Reloadtime)		
		if spareammo > 29 then
			Ammo = 30
		elseif spareammo < 30 and spareammo > 0 then
			Ammo = spareammo
		elseif spareammo == 0 then
			Ammo = 0
		end
		game.Players.LocalPlayer.PlayerGui.weapongui.ReloadingLabel.Visible = false
		
	end
end


mouse.KeyDown:connect(reload)
Gun.Deactivated:connect(stopshooting)
Gun.Activated:connect(shoot)

1 answer

✓ Accepted answer

In the part of the script

    function Damage(Part)
            if Part.Parent:FindFirstChild("Humanoid") ~= nil and Part.Parent.Name ~= Player.Name and game.Players:GetPlayerFromCharacter(Part.Parent).TeamColor ~= Player.TeamColor then
                Part.Parent.Humanoid.Health = Part.Parent.Humanoid.Health -Dmg
                Bullet:Destroy()
            end
        end

add an "else" to it. So try:

    function Damage(Part)
            if Part.Parent:FindFirstChild("Humanoid") ~= nil and Part.Parent.Name ~= Player.Name and game.Players:GetPlayerFromCharacter(Part.Parent).TeamColor ~= Player.TeamColor then
                Part.Parent.Humanoid.Health = Part.Parent.Humanoid.Health -Dmg
                Bullet:Destroy()
else
Bullet:Destroy()
            end
        end

The only problem is if the bullet spawns in the gun. You need it so it spawns close to the gun but not touching it at all.

1 comment

0

Thanks it works now :)

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