What’s the goal: I need to make the ammo replenish by one after every 0.8 seconds.
What Have I tried:
task.spawn(function()
runservice.RenderStepped:Connect(function()
--print("TimeDelta = ".. tick() - Previous)
if web.Name == "Default" and ammo < 8 or ammo == 0 then
if tick() - LastTimeUsed > 1 then
task.wait(0.8)
web:Replenish(script.AmmoCounter,CountFrame,BackFrame)
end
end
end)
end)
TL;DR: If the web class has the correct name, and the ammo is either less than 8 or 0, then wait a second to check if the elapsed time is greater than 1 seconds, then wait 0.8 seconds then replenish the ammo.
Conclusion: I know the code is far from optimal, but I’ve been staring at it so long that I fail to see anything wrong with it. Any help would be appreciated, thanks.
RunService events provide a value to avoid needing to use task.wait(). Even though this code does not have a task.wait() present it will only trigger every 0.8 seconds. This value is a very precise value of the number of seconds that has lapsed between the last RunService task.
runSerivce.RenderStepped:Connect(function(step)
local deltaTime += step --This will equal your time since the last ammo replenish
if deltaTime >= 0.8 then
deltaTime = 0 --Reset the deltaTime after a successful trigger
web:Replenish(script.AmmoCounter,CountFrame,BackFrame)
end
end)
The main reason I can’t use a loop like that is because I need it to check if you have the right gadget equipped and if your ammo isn’t equal to your maximum for that type. Also thanks for the tip about the functions not yielding the script.