Uncertain about HOW functions work

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    So when you call a function, my understanding is that you call the function and then the function runs and then once it finished running the next instruction would be be the line beneath the call.

  2. What is the issue? Include screenshots / videos if possible!
    Look below; the two code samples and my writing. I want to understand how are functions meant to work like.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Yes, but I couldn’t find any.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local function alwaysmove()
	wait(25)
	print("jpo")
end

local function ytouch(part)
	alwaysmove()
	wait(5)
	print("last jpo by 5")
end



ytouch()


As expected, when the code runs it waits 25 seconds print “jpo” and then waits 5 seconds to print “last jpo by 5”. This is normal understanding of how I expect execution of functions supposed to be like.

local function loopmove()
	while true do
		local goal = tween:Create(obj, TweenInfo.new(5)
			, {Position = Vector3.new(0,0,0),
				Color = Color3.new(0, 1, 0)})
		goal:Play()	
		local secondgoal = tween:Create(obj, TweenInfo.new(3)
			, {Position = Vector3.new(0,10,0),
				Color = Color3.new(1, 0.364706, 0.533333)})
		secondgoal:Play()

	end	
end




function Movement(part)
	print(part.Name)
	print(part.Parent:FindFirstChild("Humanoid"))
	if part.Parent:FindFirstChild("Humanoid") then

		loopmove()
		print("truthers?")
	end
end

DO NOT RUN THIS CODE. WILL HANG INDEFINITELY

emphasis this part

		goal:Play()	

I expect goal:Play() to finish executing then for the second goal to start playing with secondgoal:Play(). However, unexpectedly, goal:Play() doesn’t finish executing and immediately runs to the next line of code

When you are playing a sound or a tween, it sets “Play” to true, then immediately goes to the next line of code.

When you are playing something, it does not wait for it to end.

Tween:Play() does not yield. You must either add a wait(n) to the function, or use the Tween.Completed event.

Here’s an example:

local TweenService = game:GetService("TweenService")

local function tween(object, info, goal)
     local new = TweenService:Create(object, info, goal)
     
     new:Play()
     new.Completed:Wait()
end

How does a function which yield differ from a function that doesn’t yield - Syntax-wise. For example, when i call goal:Play() how does it not yield, does it run in coroutine(I’m confused about how threads work)?

You’re overthinking it. As @CodedJack said, the :Play() method simply sets the tween to a playing state.

If you’re interested you can find more information on the Tween object here.

Edit: I think it would be a very cool feature if they added a fourth parameter to TweenService:Create(), which would determine if the play method yields or not.