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

How do I add a reload time to my tool without yielding?

I am making a ray gun that has a magazine which the player reloads. After running into issues with server yields, I adjusted everything to run on more localized yields.

However, the issue now is that there is no wait() before the magazine is reloaded and so the player can instantly shoot again. When the function is called, we check if it has been long enough by comparing tick() to reloadtick and the reloadtime value. If I were to check the tick() again to see if it has been long enough to complete the reload, wouldn't that just return false? I'm not sure how to proceed without a wait() that would clog the server side of this gun.

local function reload(player, gun)
	
	local active = gun.gunstats.Active
		
	if active.Value == true and reloaddebounce == false then

		reloaddebounce = true
		local reloadtime = gun.gunstats.Reloadtime
		local magammo = gun.gunstats.Mag
		local maximum = gun.gunstats.Maximum
		
		local reloadtick = 0
		
		if magammo.Value < maximum.Value and tick() - reloadtick >= reloadtime.Value then --cant reload when full
			
			reloadtick = tick()
			
			changetoreload:FireClient(player)
			changecursortoreload:FireClient(player)
				
				magammo.Value = 0
			
			gun.phalanx.Magazine.Transparency = 1	
			gun.Handle.Reloadsound:Play()
				
				--wait(reloadtime.Value) --the issue is here
			
			reloaddebounce = false
			gun.phalanx.Magazine.Transparency = 0
				
				magammo.Value = maximum.Value

		end
	end
		
end
reloadevent.OnServerEvent:Connect(reload)

1 answer

Coroutines can stop yielding.

All you gotta do is on line 37 is use coroutine.wrap().

coroutine.wrap as said, just wraps the function provided in the parathesis with a coroutine.

reloadevent.OnServerEvent:Connect(coroutine.wrap(reload))

1 comment

0

Thank you, this is useful. I'm not sure I understand them very well though. When I do as you said I get an error cannot resume dead coroutine.

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