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?)
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 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.
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.
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.
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.
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
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.