How would I cancel a function without using return end?

I’m trying to end a reload function early if you unequip and I have no idea how to do it outside of the function.

If anybody knows how that’d be extremely helpful.

My reload function:

local function Reload()
	local rel = Instance.new('Animation')
	rel.AnimationId = "rbxassetid://"..module.VMReload..""
	rel = game.Workspace.CurrentCamera.Arms.Animation:LoadAnimation(rel)
	if module.Magazines > 0 then
		module.Reloading = true
		rel:Play()
		print("reloading")
		script.Parent.Handle.CoverUp:Play()
		wait(0.45)
		script.Parent.Handle.ClipOut:Play()
		local mag = script.Parent.Handle.Mag:Clone()
		mag.Parent = game.Workspace
		mag:BreakJoints()
		mag.CFrame = script.Parent.Handle.Mag.CFrame
		mag.CanCollide = true
		game:GetService("Debris"):AddItem(mag, 10)
		wait(0.85)
		script.Parent.Handle.ClipIn:Play()
		wait(0.5)
		script.Parent.Handle.BoltPull:Play()
		wait(0.15)
		script.Parent.Handle.BoltRelease:Play()
		wait(0.20)
		script.Parent.Handle.CoverDown:Play()
		wait(0.65)
		module.Ammo = module.MaxAmmo
		print("not reloading")
		module.Reloading = false
		module.Magazines = module.Magazines - 1
	end
end

Couldn’t you just Disconnect the function?

2 Likes

With the script you have you’d probably have to do a check after every wait like this.

if (script.Parent.Parent ~= localplayer.Character) then
	return
end

Though that would make the code rather messy.

You cannot “disconnect” a function, you can only disconnect an event. Therefore, if you would like to return early from a function, you would need to perform a check at every point you may want to exit. You could organize (what I’m assuming are) you animation (CoverUp, ClipOut, etc) into a table. Similar to the following:

local animSequence = {
   {Anim = script.Parent.Handle.CoverUp, Duration = 0.45},
   {Anim = script.Parent.Handle.ClipOut, Duration = 0.85},
   {Anim = script.Parent.Handle.ClipIn, Duration = 0.5},
   --And so on...
}

--Then include a for loop in your reload function like:
for i = 1, #animSequence do
   local currentAnim = animSequence[i]
   if module.Reloading then --I'm assuming this is the variable you would change to false if you want to cancel the animations
      currentAnim.Anim:Play()
      wait(currentAnim.Duration)
   else
      return --If you need do to some other cleanup outside the for loop but still in the reload function, you can use a break here
   end
end
2 Likes

Tried that. Didn’t particularly like that.

I’m going to try this. Since it looks like what I need

This doesn’t work. Whenever I turn module.Reloading then it doesn’t work at all

Then you should probably re-structure your code so that the sequence can be controlled externally. This isn’t a place to get free scripts.

I need help not free scripts. Now stop getting aggressive at me.

1 Like

Are you using Animations or Tweens? I can help you with some smaller details but like @blokav said, we can’t just provide you with a complete script. It’s up to you to make sure the implementation is correct on your end.

I’m using animations with a viewmodel.

What’s do you mean by viewmodel?

A rig stuck to the screen by CFrame

Alright. I’m thinking there’s pretty much two options at this point.

  1. You can continue the route you’re currently going with separate animations. I believe the code I gave you should be a good start but might not be working because of something else going on in the rest of your code.
  2. This problem might be easier to tackle if you merge all the smaller animations into one single animation. That way, whenever you want to cancel the reloading, you can just simply call :Stop() on it.

Alright nevermind. I’ll just at a return end before it changes the ammo.

You aren’t able to “return end”. If you want to return from a function you can simply just return and by default the returned value will be nil.

Return end works anyway so I’m not sure what you mean.

function go()
   return end
end

This will error. Maybe you have something like:

function go()
   return end

which would work… but is bad code formatting because the “end” is actually closing the function, not acting as a returned value. Lua actually sees it as:

function go()
   return
end
--This is correct code formatting and should be how you write it
-- because it will help you better understand how your scripts are
-- actually working, and maybe even eventually help you fix the
-- problem you posed in this topic.

Goodluck!

1 Like

I mean

if not enabled then reloading = false return end

works pretty well.

the end in that case is closing the conditional. Single like conditionals are alright for small statements like that, but technically the correct formatting is:

if not enabled then
   reloading = false
   return
end
1 Like