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

Help With Tools?

I have guns that reload when you press "R". The only problem is that it reloads when you press "R" even if you don't have the gun equipped. Is there any way to fix this?

Player = game.Players.LocalPlayer
Gun = script.Parent
FireSound = Gun.ShootSound
LoadSound = Gun.ReloadSound
mouse = Player:GetMouse()
enabled = true
Gun.Ammo.Value = 5
MaxAmmo = 5

function Shoot()
	function Damage(Part)
		if Part.Parent:FindFirstChild("Humanoid") ~= nil and Part.Parent.Name ~= Player.Name then
			Part.Parent.Humanoid:TakeDamage(5)
		end 
	end
	if Gun.Ammo.Value > 0 and enabled == true then
		Gun.Ammo.Value = Gun.Ammo.Value - 1
		enabled = false
		Bullet = Instance.new("Part", game.Workspace)
		Gun.GripForward = Vector3.new(0,-.2,-1)
		game.Debris:AddItem(Bullet, 2)
		Bullet.Shape = "Ball"
		Bullet.Size = Vector3.new(0.2,0.2,0.2)
		Bullet.TopSurface = "Smooth"
		Bullet.BottomSurface = "Smooth"
		Bullet.Transparency = 1
		Bullet.BrickColor = BrickColor.new("Dark stone grey")
		Bullet.CanCollide = false
		Bullet.CFrame = Gun.Handle.CFrame
		Bullet.CFrame = CFrame.new(Bullet.Position,mouse.Hit.p)
		v = Instance.new("BodyVelocity", Bullet)
		v.velocity = Bullet.CFrame.lookVector *900
		v.maxForce = Vector3.new(math.huge, math.huge, math.huge)
		FireSound:Play()
		Shell = Instance.new("Part", game.Workspace)
			Shell.TopSurface = "Smooth"
			Shell.BottomSurface = "Smooth"
			Shell.Size = Vector3.new(.1,.1,.1)
			game.Debris:AddItem(Shell, 0.8)
			Shell.CFrame = Gun.Handle.CFrame *CFrame.new(0, -.5, -.3)
		Bullet.Touched:connect(Damage)
		Light = Instance.new("PointLight", Gun)
		wait(.1)
		Light:Destroy()
		Gun.GripForward = Vector3.new(0,0,-1)
		wait()
		enabled = true
		
	end
end

function Reload(key)
	key = key:lower()
	if key == 'r' then
		if enabled == true and Gun.Ammo.Value < MaxAmmo then
			LoadSound:Play()
			wait(LoadSound.TimeLength)
			Gun.Ammo.Value = MaxAmmo
		end
	end
end

mouse.KeyDown:connect(Reload)
Gun.Activated:connect(Shoot)

4 comments

0

Can you show the script? We can't tell what we need to fix without it.

0

Updated it with the script

0

Can you show also the whole script? Where it toggles the enabled variable

0

Updated once again

1 answer

Gun.Equipped:connect(function()
--[[ THE REST OF
YOUR CODE
--]]
end)

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