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

How do you make a reloading function?

I have a reloading function in this localscript, However the gun doesnt reload. The script gets into the function because it prints Reloading but it never stops. Can someone please tell me how to fix it?

Heres my whole script and it is in a localscript:

Player = game.Players.LocalPlayer
Tool = script.Parent
Ammo = script.Parent.Configuration.Ammo.Value
BulletDamage = script.Parent.Configuration.Damage
MaxAmmo = script.Parent.Configuration.MaxAmmo.Value
mouse = Player:GetMouse()
enabled = true

function Damage(Part)
	if Part.Parent:FindFirstChild("Humanoid") ~= nil and Part.Parent.Name ~= Player.Name then
		Part.Parent.Humanoid.Health = Part.Parent.Humanoid.Health - BulletDamage.Value
		Bullet:Destroy()
	end
end

function Shoot()
	if Ammo > 0 and enabled == true then
		Ammo = Ammo -1
		enabled = false
		Bullet = Instance.new("Part", workspace)
		game.Debris:AddItem(Bullet, 2)
		Bullet.Shape = "Ball"
		Bullet.Size = Vector3.new( 0.1, 0.1, 0.1)
		Bullet.TopSurface = "Smooth"
		Bullet.BottomSurface = "Smooth"
		Bullet.BrickColor = BrickColor.new("Dark stone grey")
		Bullet.CanCollide = false
		Bullet.CFrame = Tool.Handle.CFrame
		Bullet.CFrame = CFrame.new(Bullet.Position, mouse.Hit.p)
		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 *90
		v.maxForce = Vector3.new(math.huge, math.huge, math.huge)
		Bullet.Touched:connect(Damage)
		enabled = true
	end
end

function Reload(key)
key = key:lower()
	if key == "r" then
	if enabled == true and Ammo < 30 then
	enabled = false
	repeat
		print("Reloading")
		wait(0.1)
		Ammo = Ammo +1
	until Ammo == MaxAmmo
end
end
end

mouse.KeyDown:connect(Reload)

Tool.Activated:connect(Shoot)

1 answer

try this

function reload(key)
key = key:lower()
enabled = true
if key == "r" and enabled == true and ammo < 30 then
enabled = false
repeat
print("reloading")
wait(0.1)
Ammo = Ammo + 1
until Ammo == MaxAmmo
end
end

hope it works btw i wrote this on my phone so i couldn't make the spaces

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