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

Gun Wont Reload Can I have 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 AmmoGui = plr.PlayerGui:WaitForChild("Ammo")
local AmmoL = AmmoGui:WaitForChild("AmmoL")
local mouse = plr:GetMouse()
local function shoot()
			-- Creating The Bullet
				local bullet = Instance.new("Part",game.workspace)
					bullet.Shape = "Ball"
					bullet.Size = Vector3.new(2,2,2)
					bullet.TopSurface = "Smooth"
					bullet.BottomSurface = "Smooth"
					bullet.BrickColor = BrickColor.new("Really Black")
					bullet.CanCollide = false
					bullet.Anchored = false
					--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, 5)
				-- Making Sure you lose 1 Ammo everytime you shoot.
				Ammo = Ammo - 1
end
local function reload()
	repeat 
		wait(1)
		Ammo = Ammo + 1
	until Ammo == 5
end
script.Parent.Equipped:connect(function()
	AmmoGui.Enabled = true
	AmmoL.Text = "Ammo: "..Ammo
	wait(1)
	AmmoL.Visible = true
end)
script.Parent.Unequipped:connect(function()
	AmmoGui.Enabled = false
	AmmoL.Visible = false
end)
script.Parent.Activated:connect(function()
		if Ammo > 0 then
			shoot()
	end
end)

while wait() do
		AmmoL.Text = "Ammo: "..Ammo
end
while wait() do
if Ammo == 0 then
	reload()
	end
end

No error in output my gun won't reload. Help?

1 answer

Your infinite loop on line 51 is stopping the reloading loop from ever being read. But really, you should check if the gun needs to reload after firing.

Line 33;

local plr = game.Players.LocalPlayer
local Gun = script.Parent
local Ammo = 5
local AmmoGui = plr.PlayerGui:WaitForChild("Ammo")
local AmmoL = AmmoGui:WaitForChild("AmmoL")
local mouse = plr:GetMouse()

function reload()
	repeat wait(1) Ammo = Ammo + 1
	until Ammo == 5
end

function shoot()
	-- Creating The Bullet
	local bullet = Instance.new("Part",game.workspace)
	bullet.Shape = "Ball"
	bullet.Size = Vector3.new(2,2,2)
	bullet.TopSurface = "Smooth"
	bullet.BottomSurface = "Smooth"
	bullet.BrickColor = BrickColor.new("Really Black")
	bullet.CanCollide = false
	bullet.Anchored = false
	 --CFrame Stuff
	bullet.CFrame = Gun.Handle.CFrame 
	 bullet.CFrame = CFrame.new(bullet.Position,mouse.Hit.p)
	--BodyVelocity 
	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, 5)
	-- Making Sure you lose 1 Ammo everytime you shoot.
	Ammo = Ammo - 1
	-- Check if you need to reload
	if Ammo == 0 then
		reload();
	end
end

script.Parent.Equipped:connect(function()
	AmmoGui.Enabled = true
	AmmoL.Text = "Ammo: "..Ammo
	wait(1)
	AmmoL.Visible = true
end)

script.Parent.Unequipped:connect(function()
	AmmoGui.Enabled = false
	AmmoL.Visible = false
end)

script.Parent.Activated:connect(function()
	shoot()
end)

while wait() do
	AmmoL.Text = "Ammo: "..Ammo
end

1 comment

0

Ha I didnt give you the whole script lol I closed them on my end imm sorry

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