How can i chain my methods?

  1. What do you want to achieve? Keep it simple and clear!

So basically. Im making my own “Wait” module with methods like “Wait,WhenReached,Set and CancelWait”

  1. What is the issue? Include screenshots / videos if possible!

My problem occurs at the wait method. How can i have the thread yield but at the same time also chain the methods shown below:

local WaitModule = require(game.ServerScriptService.ModuleScript).new()



WaitModule.Wait(5):WhenReached(5):Set(5)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I did ask something like this before which i got a solution for. But that solution doesnt quite work here. I also did some research before posting how to “Chain methods”. But found nothing.

I know that this is possible because ive seen this before in a code. Sadly my OOP skills arent the greatest. I know a decent amount but not that much to know how to chain methods xd.

But any help would be really cool because im stuck here obviously.

1 Like

Return the object that the function is being called on, and it should work.

1 Like

Already doing it. But the problem occurs at the wait method. It will yield the code before proceeding to the other methods

Is that not what it’s supposed to do? What is the intended behavior? An expected output might be helpful.

1 Like

Alr my bad maybe my explanation was a bit bad. Basically:

Code fires the “Wait” method BUT dont yield it. Chain togheter the other methods before. After they have been chained yield the code.

Sorry if my explanation isnt the greatest. English isnt my main language so when it comes to explaining stuff its a bit hard :sweat_smile:

Heres the full module code in case you need it. Still a WIP though

local CustomWait = {}
CustomWait.__index = CustomWait





function CustomWait.new()
	return setmetatable(CustomWait,{Cancel = false})
end



function CustomWait.Wait(Seconds: number)
	
	local Cancel = getmetatable(CustomWait).Cancel
	local Start = os.clock()
	
	repeat game["Run Service"].Heartbeat:Wait() print("Hello") until os.clock() - Start >= Seconds or Cancel
	
	return CustomWait
end




function CustomWait:WhenReached(Second: number)
	print("Sup")
	return CustomWait
end



function CustomWait:CancelWait()
	
end



function CustomWait:Set(Second: number)
	
end





return CustomWait

What’s wrong with just switching the execution order around to

WaitModule:WhenReached(5):Set(5).Wait(5)
1 Like

I don’t know how to solve your problem. But what I have gathered is to use coroutine.yield and coroutine.running for this case.

Not the best with coroutines and still learning them but i’m getting there.

1 Like

That could work i guess never thought of that. Let me try it ill get back to you in a bit

KK il mark this as solved. I fixed it. I realized i was just overcomplicating stuff.

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