Problems overwriting Attribute/Walkspeed

For a long time, either when working on a combat system or literally anything else, I’ve wondered how to be able to stop a function that is meant to change a certain value after a time. I found the following post from someone that somewhat has my problem:

The first answer to the post is something I can put into practice, but when I actually tried I failed. Example of what I’m looking for:

local value = 1

function ChangeValue()
      task.wait(2)
      value = 2
end)

function ChangeValue2()
       task.wait(4)
       value = 1
end)

ChangeValue() -- Let's say I run my function meant to overwrite the variable 'value', but then if a condition is met, I change my mind and for example, I decide that I wanna make the wait between changing the value longer, how would I be able to stop the wait of the function and never actually set my variable to two? 

If number == 1 then
      ChangeValue2()
end

This code example isn’t optimal to explain my point, since I this isn’t a hard problem to come out with a solution for, but is when I want to make the player unable to walk if they’re stunned or it is when I wanna change an attribute after a certain time. I know I must use coroutines, but how would I apply that to what I want? I don’t want the code for it, I just want an explanation how to do it (Since I don’t fully understand coroutines even after researching about it for 3 days I fail to put into practice what I learned and what I understood from the post I addressed earlier)

To better explain what I want:

function Framework:Add()
    local Humanoid = ...
    Humanoid:SetAttribute("Value", Humanoid:GetAttribute("Value") + 1)

	if Humanoid:GetAttribute("Value") > 99 then
		spawn(function()
      -- Here I should find a way to yield the coroutine I had created on the example function			
			Humanoid.WalkSpeed = 10
			Humanoid.JumpPower = 0
			task.wait(2.5)
			Humanoid.WalkSpeed = 16
			Humanoid.JumpPower = 50
		end)
	end
end

function Framework:Example()
     coroutine.wrap(function()
            task.wait(2)
            Humanoid:SetAttribute("Value", 1)
     end)()

     self:Add()
end

Now, how can I make the coroutine I just created on the example function “global” so that the function add can just remotely yield it to stop the wait from setting my value to 1 if the attribute is bigger than 99, which is not exactly what I’m looking for.

Welp… it was as simple as using coroutine.create instead of .wrap in order to be able to close the coroutine…

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.