Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I get loop to change the values in the tool?

Asked by 2 years ago

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

Log in to vote
0
Answered by 2 years ago

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.

0
Thank s for that. But I think the problem is in a different script Boss246813579 -6 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

Its a different script that is causing the problem

Answer this question