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

How do I make this pistol reload when the R key is pressed, and also show the amount of ammo?

So the problem resides with line 52 and line 58.

I'm trying to make it so once a shot is fired the tools name turns to the word Pistol and then the amount of ammo.

I'm also trying to make it to where when you press the R key it reloads. This works but after I press R I need to click the mouse in order to reload.

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local ammo = 8
local isReload = false
local mouse = game.Players.LocalPlayer:GetMouse()




tool.Equipped:connect(function(mouse)
    print("Pistol Equipped")
    mouse.Button1Down:connect(function()
	print("Mouse Clicked")
    if (not isReload) and ammo > 0 then --if Player is not reloading
            ammo = ammo - 1    
            local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 200)
            local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

			local beam = Instance.new("Part", workspace)
            beam.BrickColor = BrickColor.new("Gray")
            beam.FormFactor = "Custom"
            beam.Material = "Neon"
            beam.Transparency = 0.75
            beam.Anchored = true
            beam.Locked = true
            beam.CanCollide = false

			local light = Instance.new("PointLight", beam)
			light.Range = 15

            local distance = (tool.Handle.CFrame.p - position).magnitude
            beam.Size = Vector3.new(0.3, 0.3, distance)
            beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, - distance / 2)

            game:GetService("Debris"):AddItem(beam, 0.1)

            if part then
                local humanoid = part.Parent:FindFirstChild("Humanoid")

                if not humanoid then
                    humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
                end

                if humanoid then
                    humanoid:TakeDamage(30)
                end
        end
    else
        tool.Name = "Reloading"
                isReload = true
                wait(2)
                tool.Name = ("Pistol " .. ammo) -- Name of tool is "Pistol (amount of ammo)
                ammo = 8
                print("Reloaded")
                isReload = false
    end
		
		mouse.KeyDown:connect(function (r) --Reloading with r key Function
			isReload = true
		end)
		
    end)
end)


Sorry if this doesn't make sense.

1 answer

✓ Accepted answer

That's not how mouse.KeyDown works, but I'd recommend using UserInputService anyway, as mouse.KeyDown is deprecated.

Everything is explained in the script:

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local ammo = 8
local reloading = false
local mouse = game.Players.LocalPlayer:GetMouse()

function reload(amount) --// Create a reload function to avoid retyping.
	tool.Name = "Reloading"
	reloading = true
	wait(2)
	ammo = amount
	tool.Name = "Pistol " .. tostring(ammo) --// Show the new amount of ammo.
	print("Reloaded")
	reloading = false
end


tool.Equipped:connect(function(mouse)
	print("Pistol Equipped")
	game:GetService('UserInputService').InputBegan:connect(function(input, gameproccessed) --// Get UserInputService, connect a function.
		if input.KeyCode == Enum.KeyCode.R then --// If the key pressed was R,
			reload(8) --// Call the reload function.
		end
	end)
	mouse.Button1Down:connect(function()
		print("Mouse Clicked")
		if (not reloading) and ammo > 0 then
			ammo = ammo - 1
			tool.Name = "Pistol " ..tostring(ammo) --// Show the new amount of ammo.
			local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 200)
			local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
			
			local beam = Instance.new("Part", workspace)
			beam.BrickColor = BrickColor.new("Gray")
			beam.FormFactor = "Custom"
			beam.Material = "Neon"
			beam.Transparency = 0.75
			beam.Anchored = true
			beam.Locked = true
			beam.CanCollide = false
			
			local light = Instance.new("PointLight", beam)
			light.Range = 15
			
			local distance = (tool.Handle.CFrame.p - position).magnitude
			beam.Size = Vector3.new(0.3, 0.3, distance)
			beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, - distance / 2)
			
			game:GetService("Debris"):AddItem(beam, 0.1)
			
			if part then
				local humanoid = part.Parent:FindFirstChild("Humanoid")
				if not humanoid then
					humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
				end
				if humanoid then
					humanoid:TakeDamage(30)
				end
			end
		else
			reload(8) --// Call the reload function.
		end
	end)
end)

Remember, if you don't know how to do something, don't be afraid to look it up on the wiki. Hope this helped.

1 comment

0

Bout time you actually did something useful with ye life

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