If I wanted to stop a sword from slashing someone if for instance the sword hit a rock. Which what you have in mind and are trying to negate is called continuous collision detection. Then you would do an attack. Check to see what you hit, and then proceed accordingly. This can be regular conditionals, a finite state machine, or you could constantly monitor a value. As I said many solutions exist.
You are claiming that Roblox doesn’t allow you to solve this problem. And I am saying that the problem you presented is the issue in the first place.
I don’t find this description detailed enough. I also don’t think you read everything that has been said about this, as I feel we have now rolled the pages back to earlier stages. This is about the slash function continuing to run because of multiple yields going on throughout the slash function, and stuff like humanoid properties and variables being changed throughout. This cannot continue once I interrupt the slashing, and stopping the slash function doesen’t cut it. I think you spent more time writing about collision detection rather than the actual issue. Like I mentioned, script examples would help and I invite you to enlighten me with it.
Sorry if I am wrong here but why do you need big scripts if anything they make it harder to find certain bits in the code. and edit the script. And also if there is a bug in the script the entire script will stop working unlike if you have multiple scripts.
Not sure if you mean that you use multiple scripts.
You are 100% correct, we rolled back and seemingly is repeating everything @Intended_Pun mentioned, because what he said is correct.
You do not need to stop the function. You need to break up your function into many parts and be more abstract about the problem you are trying to solve. Instead of stopping a function midway through. The function itself should be designed to be completed in some way shape or form. And some other function should pickup the slack where that one left off.
Multiple functions running together is what makes a system.
Here is literal code that will accomplish what you want. Just set swinging to false when you’re done and the loop will stop working. You can add debounces to make sure the loop only plays the action once.
local swinging = false
function swing()
swinging = true
--Wait until swinging is done...
swinging = false
end
while wait() do --Using wait() instead of swing so I can use one loop.
if swinging == true then
--Do swinging stuff.
end
end
Yes, and I posted the if statement solutions earlier, and mentioned the repercussions of it. I don’t see why the function has to complete no matter what, often it doesen’t, sometimes ROBLOX does it for you if the script is destroyed or something.
The concept I shared would certainly work with your example. I don’t see why you won’t even try to implement the ideas we’re giving you. If multiple people are saying that your implementation is not the best way, you should seriously consider a new one. You are hellbent on stopping threads, but that ultimately has nothing to do with what you are trying to achieve.
As a programmer, it is always important to think outside of the box and have an open mind. Otherwise, you will never improve.
Perhaps I did it incorrectly and miss understood, can you apply my example to yours and show me in that case? I am hellbent on unwanted code not running, and I refuse inefficient ideas. This is not unreasonable, even if some people would like it to be. I don’t refuse help, I refuse answers that might just not be answers.
If I don’t like someones answer and I am confident it does not suit what I am doing, then it is perhaps someone else who is going on the defense. Calling this unreasonable is unreasonable.
There are plenty of way to accomplish what you want, many of them have been listed here. Roblox rarely changes core lua structures for a variety of complicated reasons.
Several talented programmers have told you that there are alternate ways to approach this problem. You are defending your idea to the point where you are coming off as rude. To solve your problem I suggest taking another look at @Intended_Pun’s code.
It’s okay, we’ve all been there at one point in time. It’s just important to realize that when you’re asking for help, that you’re gonna get all types of answers and you should keep in mind that some people may know more than you do. So when you question the people that do know, despite yourself not knowing or understanding fully it becomes hard to get the right answers.
I’ve asked this question before in a similar fashion and I received the same response. I changed my design and that was all I needed for things to work out. You are not supposed to stop a function call. There are several ways to tackle this problem - from something as simple as making your system event-based to using a finite state machine and more.
Your concept is fundamentally wrong and if you aren’t willing to try the solutions people provide you or see the fault in your design, you don’t deserve the help you’ve gotten until now. What is the major problem in changing your design? You have an odd apprehension to doing so and would rather sacrifice functionality for what, prettiness that no one will see?
If you provided an exact use case that isn’t entirely ambiguous and isn’t a weak example then we would be able to tell you whether an alternative solution exists or not. An alternative likely does exist, as no one seems to have had this problem before, but if you provide an actual use case with code to show how you would go about performing that use case it would help everyone understand what you’re looking for.
A coroutine.destroy() is not implemented because the actual solution is to remove the reference.
Just yield the coroutine if a conditional isn’t true, remove the last reference to the thread, and you can let the garbage collector destroy the thread for you.
local touched = false
local Coroutine1 = coroutine.create(function()
print("START")
wait(4)
if touched then coroutine.yield() Coroutine1 = nil end
print("MIDDLE")
wait(3)
if touched then coroutine.yield() Coroutine1 = nil end
print("END")
end)
coroutine.resume(Coroutine1)
script.Parent.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild("Humanoid") and touched == false then
touched = true
print("TOUCHED")
end
end)
(Edit: you may also wish to assign the “destroy” method to an anonymous function that connects to a bindable event within scope that also removes its own reference. You can then call the kill externally, if desired.)
I like to use an iterator of sorts to do this kind of thing.
LastTime = 0
local function YieldFunction()
local CurrentTime = tick()
LastTime = CurrentTime
wait(5)
if LastTime ~= CurrentTime then
return
end
print("not cancelled")
end
local function OnEvent()
LastTime = CurrentTime
end