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 get loop to change the values in the tool?

I am making a lazier gun which rechargers over time if you have fired it at least once.

Here is the code I got for recognizing the guns Ammo and MagSize is lower to 6. If it is its suppose to run the loop and plus 1 onto the Ammo and MagSize values every 2 seconds till till it reaches 6 again.

local Ammo = script.Parent.Parent.Configs.Ammo.Value
local MagSize = script.Parent.Parent.Configs.MagSize.Value
local Tool = script.Parent.Parent

Tool.Equipped:Connect(function(Recharge)
		
		if Ammo < 6 and MagSize < 6 then
			Recharge = true
			end
		end)


while Recharge ==true do

	if Ammo >= 6 and MagSize >= 6 then
		Recharge = false
	else
	Ammo = Ammo +1 
	MagSize = MagSize+1
	wait(2)	
	end
end

PS the value Ammo is for the gui display, while MagSize is the guns ammo. There both a set value stored in a separate folder for easy access later.

2 answers

✓ Accepted answer

It's probably not, but it might be an issue with the variable Recharge. There are also a few ways to shorten your code:

local Tool = script.Parent.Parent
local Ammo = Tool.Configs.Ammo.Value
local MagSize = Tool.Configs.MagSize.Value

Tool.Equipped:Connect(function()

  while Ammo<6 and MagSize<6 do
     Ammo = Ammo +1 
     MagSize = MagSize+1
     wait(2) 
  end
end)

Also, I see that you had the recharging separate from equipping the tool, so if you still want that you could remove the Tool.Equipped event.

1 comment

0

Thank s for that. But I think the problem is in a different script

Its a different script that is causing the problem

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