Am I able to make a running script with closing and spawning threads?

So I’ve made a script that has this line in it

function CalibrateMovement(actionName, inputState, _inputObject)
	if actionName == Action_RUN then
		if inputState == Enum.UserInputState.Begin then
			print("Running")
			
			task.spawn(RunThread)
			task.cancel(WalkThread)
			
			
		elseif inputState == Enum.UserInputState.End then
			print("Walking")
			
			task.spawn(WalkThread)
			task.cancel(RunThread)
			
			
		end
	end
end

The Walkthread lowers the player’s walk speed till it hits the desired amount and the RunThread is the same thing but reversed.
However, if you could tell just by looking at these, that the script wouldn’t let me cancel either thread after spawning it, causing me to get this error
invalid argument #1 to 'cancel' (thread expected, got function)

How can I be able to cancel the walk/run thread? What am I doing wrong?

How are you defining the RunThread and WalkThread?

This is my first time messing with canceling or spawning threads, so please correct me if I’m using it wrong.
But the Walk and Run threads look like this.

local RunThread = function()
	
	repeat
		wait()
		Humanoid.WalkSpeed += SpeedIncerments
		print("+",Humanoid.WalkSpeed)
	until Humanoid.WalkSpeed >= MaxRunSpeed
	
	Humanoid.WalkSpeed = MaxRunSpeed
end

local WalkThread = function()

	repeat
		wait()
		Humanoid.WalkSpeed -= SpeedIncerments * 1.5
		print("-",Humanoid.WalkSpeed)
	until Humanoid.WalkSpeed >= MinWalkSpeed

	Humanoid.WalkSpeed = MinWalkSpeed
end

I know these are functions, and I was wondering how I would convert it into a thread

RunThread and WalkThread are both of type function, when they need to be of type Thread.
One way to create them as threads is to do this:

local RunThread = coroutine.create(function()
	
	repeat
		wait()
		Humanoid.WalkSpeed += SpeedIncerments
		print("+",Humanoid.WalkSpeed)
	until Humanoid.WalkSpeed >= MaxRunSpeed
	
	Humanoid.WalkSpeed = MaxRunSpeed
end)

local WalkThread = coroutine.create(function()

	repeat
		wait()
		Humanoid.WalkSpeed -= SpeedIncerments * 1.5
		print("-",Humanoid.WalkSpeed)
	until Humanoid.WalkSpeed >= MinWalkSpeed

	Humanoid.WalkSpeed = MinWalkSpeed
end)
1 Like

Well, I’ve tried that before. However, after press V “Which starts the run thread” it works fine, but, when you let go of V the walk thread does work as intended, as it doesn’t lower the walk speed back down

It does work fine, you just have a logic error in your code!

You have until Humanoid.WalkSpeed >= MinWalkSpeed when it SHOULD be until Humanoid.WalkSpeed <= MinWalkSpeed

1 Like

This is a picture of my output
image

It first says running because it fires the run thread and +'s the number on the walk speed.

I switched that problem just now “I somehow didn’t see that! Thanks”. But it still has the same output.
In the output I want to see

Walking
- 16.6
- 16.5

And so on

whenever you want to close a thread but want to run it again, you have to define it again after closing it

function CalibrateMovement(actionName, inputState, _inputObject)
	if actionName == Action_RUN then
		if inputState == Enum.UserInputState.Begin then
			print("Running")
			
			task.spawn(RunThread)
			task.cancel(WalkThread)
			
			WalkThread = coroutine.create(function()
				repeat
					task.wait()
					Humanoid.WalkSpeed -= SpeedIncerments * 1.5
					print("-",Humanoid.WalkSpeed)
				until Humanoid.WalkSpeed <= MinWalkSpeed
				
				Humanoid.WalkSpeed = MinWalkSpeed
			end)
		elseif inputState == Enum.UserInputState.End then
			print("Walking")
			
			task.spawn(WalkThread)
			task.cancel(RunThread)
			
			RunThread = coroutine.create(function()
				repeat
					task.wait()
					Humanoid.WalkSpeed += SpeedIncerments
					print("+",Humanoid.WalkSpeed)
				until Humanoid.WalkSpeed >= MaxRunSpeed
				
				Humanoid.WalkSpeed = MaxRunSpeed
			end)
		end
	end
end

And also change the threads to this

local RunThread = coroutine.create(function()
	repeat
		task.wait()
		Humanoid.WalkSpeed += SpeedIncerments
		print("+",Humanoid.WalkSpeed)
	until Humanoid.WalkSpeed >= MaxRunSpeed
	
	Humanoid.WalkSpeed = MaxRunSpeed
end)

local WalkThread = coroutine.create(function()
	repeat
		task.wait()
		Humanoid.WalkSpeed -= SpeedIncerments * 1.5
		print("-",Humanoid.WalkSpeed)
	until Humanoid.WalkSpeed <= MinWalkSpeed
	
	Humanoid.WalkSpeed = MinWalkSpeed
end)
1 Like

So I only have to redefine the threads because I was removing them instead of, for say, stoping them?

Okay, that fixed it. Thank you both for the help!

1 Like

No, you have to redefine them becuase a closed thread cannot be run again, so if you use a close thread in task.spawn or coroutine.resume it will error.

Redefining the threads will replace the closed one with its resumable state again.

2 Likes

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