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

When I shoot the gun I lose no health Help?

-- Script Made By johndeer2233 For A Application To Make A Game
local plr = game.Players.LocalPlayer
local Gun = script.Parent
local Ammo = 5
local MaxAmmo = 5
local AmmoGui = plr.PlayerGui:WaitForChild("Ammo")
local AmmoL = AmmoGui:WaitForChild("AmmoL")
local mouse = plr:GetMouse()
local Shot = Instance.new("Sound",script.Parent.Part)
Shot.Name = "Shot"
Shot.SoundId = "rbxassetid://131070686"

local Reload = Instance.new("Sound",script.Parent.Part)
Reload.Name = "Reload"
Reload.SoundId = "rbxassetid://317130715"
Reload.Looped = true
Reload.Volume = 10
local function shoot()
			-- Creating The Bullet
				bullet = Instance.new("Part",game.workspace)
					bullet.Shape = "Ball"
					bullet.Size = Vector3.new(0.5,0.5,0.5)
					bullet.TopSurface = "Smooth"
					bullet.BottomSurface = "Smooth"
					bullet.BrickColor = BrickColor.new("Really Black")
					bullet.CanCollide = false
					bullet.Anchored = false
					bullet.Name = "Bullet"
					--CFrame Stuff
					bullet.CFrame = Gun.Handle.CFrame 
					bullet.CFrame = CFrame.new(bullet.Position,mouse.Hit.p)
					--body velocity	
					local v = Instance.new("BodyVelocity",bullet)
					v.velocity = bullet.CFrame.lookVector * 90
					v.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
					game.Debris:AddItem(bullet, 3)
					Ammo = Ammo - 1
					--Sound Management
					Shot:Play()
					local fire = Instance.new("Fire", script.Parent.Part)
					fire.Name = "Fire"
					fire.Heat = 1
					fire.Size = 2
					local smoke = Instance.new("Smoke",script.Parent.Part)
					smoke.Name = "Smoke"
					smoke.Opacity = 1
					smoke.RiseVelocity = 0.1
					smoke.Size = 0.1
					wait(1.5)
					fire:Destroy()
					smoke:Destroy()
					Shot:Stop()
	end
				-- Making Sure you lose 1 Ammo everytime you shoot.
local function reload()
		wait(1)
	repeat
		if Reload.IsPlaying == true then
			AmmoL.Text = "Ammo: "..Ammo	
			Ammo = Ammo + 1
		else
			Reload:Play()
			AmmoL.Text = "Ammo: "..Ammo	
			Ammo = Ammo + 1
		end	
		wait()
	until Ammo == 5 
	Reload:Stop()
	Ammo = 5
end
script.Parent.Equipped:connect(function()
	AmmoGui.Enabled = true
	AmmoL.Text = "Ammo: "..Ammo
	wait(1)
	AmmoL.Visible = true
	
	bullet.Touched:connect(function(hit)
			local plr = hit.Parent
			print(plr)
			local plrn = plr.Name
			print(plrn)
				if game.Players:FindFirstChild(plrn) then
					print("Stage1")
					local h = game.Players:FindFirstChild(plr).Character:FindFirstChild("Humanoid")
						if h then
							h:TakeDamage(10)
							print("Stage2")
			end
		end
	end)
end)
script.Parent.Unequipped:connect(function()
	AmmoGui.Enabled = false
	AmmoL.Visible = false
end)
script.Parent.Activated:connect(function()
	if Ammo > 0 then
		shoot()	
	elseif Ammo == 0 then
		reload()
		end
end)
while wait() do
	AmmoL.Text = "Ammo: "..Ammo
		if Ammo > MaxAmmo or Ammo < -1 then
			Ammo = 5
	end
end

no errors other then Players.Player1.Backpack.Gun.MainScript:77: attempt to index global 'bullet' (a nil value) and that is just because Bullet does not exist until you shoot. Help?

1 comment

0

If its for an application you shouldn't be asking. Your technically cheating with this application then, and i'm just warning you, that might have a big effect later on, when you fail to trouble shoot some errors.

1 answer

✓ Accepted answer

Your error is much worse than you think. Firstly, something about errors, they pretty much break everything. There aren't really "ignorable" errors in Roblox, at least in this case when you don't have much code, and no error handling.

That being said, you misunderstand how the gun works. Each time you shoot, you are creating a new bullet instance. That means there are multiple bullets, each one of which you want to connect a Touched event listener to.

You would want to put the bullet.Touched:connect(...) code (around line 77ish) in the shoot() function.

3 comments

0

I tried that

0

I'll try again. though

0

I am 100% sure you need to do that. If it still doesn't work, something else is the matter.

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