Issue With Ending A Function

The last issue in my weapons system is that I’m trying to stop a function from another function inside of it. If I try to “return”, it just ends the internal function. How could I have this also end the parent function? I could convert this to disabling from an outside function if easier.

Code:

-- Reload function:

ReloadRemote.OnServerEvent:Connect(function(Player, WeaponName) -- When ReloadEvent is fired:
	
	if Gun.Name == WeaponName then -- If this gun is the one equipped:
		
		local Char = game.Workspace:FindFirstChild(Player.Name) -- Set char to player's character.
		local Ammo = Char.Ammo[GunStats.AmmoType] -- Set ammo to guns AmmoType.

		if Gun.Ammo.Value < GunStats.Ammo and ReloadDebounce == false then -- If gun has less ammo than its max:

			local Connection = coroutine.create(function()
				
				while wait() do
					
					if ReloadCancel == true then
						
						ReloadCancel = false
						
						-- End reload remote function				
						
					end
				end
			end)

Just the relevant snippet.

1 Like

@Phazezero9 before you respond, I’ve found a solution but also ran into a different error, mind if I share that with you? If you have a different solution than coroutines then by all means go ahead first.

3 Likes

My issue is that the coroutine doesn’t fire even before stopped. It only prints that the first event was triggered, but never that the coroutine was. Why would the coroutine not even start running, as it should at least print before it can stop.


ReloadRemote.OnServerEvent:Connect(function(Player, WeaponName) -- When ReloadEvent is fired:
    
    print("FiredReload")
    
    local Reload;Reload = coroutine.create(function()
        print("Coroutine fired")
        if Gun.Name == WeaponName then -- If this gun is the one equipped:

            local Char = game.Workspace:FindFirstChild(Player.Name) -- Set char to player's character.
            local Ammo = Char.Ammo[GunStats.AmmoType] -- Set ammo to guns AmmoType.

            if Gun.Ammo.Value < GunStats.Ammo and ReloadDebounce == false then -- If gun has less ammo than its max:

                local Connection = coroutine.create(function()

                    while wait() do

                        if ReloadCancel == true then

                            ReloadCancel = false
                            
                            coroutine.close(Reload)
                            
                            return            

                        end
                    end
                end)

Code Snippet ^^ @Phazezero9 @Numbnut728282

I don’t have much experience working with coroutines but I found a snippet in the documentation that may be relevant:

Once created, a coroutine doesn’t begin running until the first call to coroutine.resume() which passes the arguments to the function.

At coroutine | Documentation - Roblox Creator Hub

1 Like

Checking the state of a variable in an infinite loop is a bit of a code smell, you’d likely be better off creating an event using a BindableEvent - and in the process may avoid this issue entirely!

How would I do this? The main function is already event triggered and reload cancel is changed by a bindable event.

It would help if you shared the rest of the function. Are you trying to set the ammo count after a delay unless the reload was cancelled? If so, you can set a flag when the bindable event fires that you check after a task.wait before setting the ammo. You could also use task.delay and cancel the coroutine it returns when the reload is cancelled.

Relevant docs:
task | Documentation - Roblox Creator Hub
coroutine | Documentation - Roblox Creator Hub