Can ROBLOX make it easier for us to halt a function?

Let’s say I wanted to halt this function after it came to middle (let’s say 5 seconds after the function began). How would I do this?

function()
    print("START")
    wait(4)
    print("MIDDLE")
    wait(3)
    print("END")
end

Seems so simple, and yet very difficult to accomplish to my confusion. Here is something I tried,

local Coroutine1 = coroutine.create(function()
	print("START")
	wait(4)
	print("MIDDLE")
	wait(3)
	print("END")
end)

coroutine.resume(Coroutine1)

local Touched = false
script.Parent.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") and Touched == false then
		Touched = true
		print("TOUCHED")
		coroutine.yield(Coroutine1)
	end
end)

I realized I might be using coroutine.yield incorrectly however, yet I have tried in nearly every other way I can think of (yes, using coroutine.yield() inside the coroutine and more) and I am unable to accomplish exactly what I’m trying to do (please post if you believe you have specific a solution). Destroying the script/disabling it entirely is also not an option.

Quite frankly I don’t believe there is any actually good solution to doing this and I’m really wondering why there is no good way of just destroying a thread (e.g coroutine.destroy(thread)?). I have a lot of unnecessary, additional, unwanted code running in my script because it depends on some functions not continuing, coming to a stop so I don’t have to deal with random errors popping up later on. The best I have found is some half-working, hacky coroutine.kill function I found on this forum which won’t work in every capacity and could be simpler to use.

… why is something like this not implemented? (I believe ROBLOX has something like this happening when the script is destroyed so can’t be difficult?)

You just want the script to stop running, permanently? Why can’t you disable it?

What’s your use case? I’ve never needed anything like this. You could have a loop that handles the logic, and on touch, it stops the loop?

1 Like

I stated I want a function to stop running permanently. No, I can’t disable the script.

Like @Semaphorism said, why would you need to halt a function in the first place?

If you’re looking to stop a function you can just call return?

6 Likes

Imagine making a medieval sword, now let’s say it has a slash function for instance. What if you have this sword hit a rock. Being rock hard, it makes the players sword bounce back and the player stunned. In this case, I have to interrupt the ongoing slash function, as it is still running throughout the slashing. However I just interrupted it by smashing the sword into a rock and decided I want the player with the sword to bounce back. The slash function will complete, and continue with unnecessary code.

I don’t know specifically when the function has to end, which renders return useless for this case. (I’d be happy to be proven wrong with an example)

I don’t exactly understand what you’re trying to say, but what I make out is that you want a denounce for when you don’t want a sword to play?

If this is the cause you could do something like creating a Boolean and checking if it’s true / false and return causing the function to stop / unable to start.

It’s more complicated than that. Just forget the slashing thing and think of how you’d stop the example I provided above.

I mean, that’s a weak use case. I find it hard to believe that terminating your thread randomly is going to be the best solution here. I don’t know what kind of code you’re going to be running, but you could easily stop the animation playing and thus make it appear like the sword bounced back when the rock gets touched.

Otherwise, you could literally put the function in its own script and disable that script at will when needed. I wouldn’t even consider doing that if it were my game though.

1 Like

If you don’t want the function to keep running after wait resumes then check if the condition that indicates the function should not keep running is true and stop if it is from inside the function. Other contexts shouldn’t be able to mess with it, you can handle everything from inside.

4 Likes

This is what I thought of instantly, but in my script, it means I have to spam if statements very often, making it essentially a disgusting script, and also makes it seem inefficient, because I’d be having a lot of waits in the air, currently yielding the script and waiting for the if statement to basically tell the script nothing is supposed to happen.

I cannot put the function in its own script. I can’t really create big scripts this way. Also terminating a thread is good if you just do all the cleanup after it. Which is more efficient than spamming if statements.

1 Like

I agree that putting the function in its own script would be messy, but why do you need to stop the code running in the first place?

As I already said,

You could easily stop the animation playing and thus make it appear like the sword bounced back when the rock gets touched.

There’s no reason to get fancy here.

If you focus too much on whether a script is bad looking, you’re going to waste time, just write functional scripts, don’t worry if its too “disgusting”. Then later on optimize and rewrite if necessary. Likely what you should do, is the slash function should be a loop of some sort that will break, something like

local stopped = false -- change this when you want to stop it
function slash()
  for i=1,10 do
    if stopped then break end
    -- do slash attack in increments 
    wait() -- wait some time between increments
  end
end
1 Like

You’re thinking of it in too simple context. We don’t just have an animation running, we got stuff like the humanoid properties changing while I’m slashing, and got numerous waits which over the course of the function changes the properties, also other variables in the script, things which just can’t continue once a sword is bounced back. There is a good reason to get fancy here. The code has to stop running. No bypasses.

Part of my point, is I could make this script possibly disgusting looking and laggy, but why do I need to needlessly sacrifice this when they can implement something like this?

This is not just “muh script isnt nice looking!” it’s just inefficient coding.

As others have said, you don’t really want to stop a function, instead stop the animation from playing. Also, this should be put as a feature request.

Read my other post two below that.