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 a RayCast gun reload?

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer

tool.Equipped:connect(function(mouse)
	print("Pistol Equipped")
	
	mouse.Button1Down:connect(function()
		print("Mouse Clicked")
		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 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
			--reloading
					for i=8, -1 do	
			if i>0 then
				print("Ammo")
			else
				tool.Name = "Reloading"
				wait(2)
				tool.Name = "Pistol"
				i=8
				print("Reloaded")
				break
			end
			end
			--done reloading
					
			if humanoid then
				humanoid:TakeDamage(30)
			end
		end
	end)
end)

Trying to make the gun have 8 bullets and reload in 2 seconds. Whilst reloading the name becomes Reloading then switches back to Pistol.

I think the problem is at connecting the "i" variable in the for loop with something, but I don't know what that something is or how to do it. Thanks.

1 answer

✓ Accepted answer

You're using a for loop where one isn't necessary. All you need to do is set an ammo variable, change it on click, and check it with an if statement.

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local ammo = 8 --// Set the ammo variable.

tool.Equipped:connect(function(mouse)
	print("Pistol Equipped")
	mouse.Button1Down:connect(function()
		print("Mouse Clicked")
		ammo = ammo - 1 --// I assume you want the ammo to decrease even if nothing was hit; if not you can move this to wherever you see fit.
		--// Rest of the code still goes here, just cutting it down to the relevant bits.
		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
		if ammo > 0 then --// We check the ammo, even if it hasn't hit anything. Once again, move this into the above if statement if you prefer that.
			print("Ammo")
		else
			tool.Name = "Reloading"
			wait(2)
			tool.Name = "Pistol"
			ammo = 8
			print("Reloaded")
		end
	end)
end)

In the future, please be more descriptive about your problem. People are more likely to answer if they don't have to guess where the issue is.

You should check out the wiki page on loops. Though they aren't necessary for this problem, they are very useful and you'll want to know how to set them up.

Hope this helped.

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