How to tell when a method is complete

So basically i have Customer class with Walk method

function CustomerClass:Walk()
	print(self.Name, " walks")
	local goal = {}
	goal.Position = Vector3.new(-10.5, 0.5, 0.5)
	
	local tween = TweenService:Create(self.Model, tweenInfo, goal)
	tween:Play()
	
	tween.Completed:Connect(function()
		print(self.Name, "arrived")
	end)
end

How do I determine that the Walk method is complete and run another method on server script,
i tried return but it doesn’t work

Use :Wait() on the end of your tween.Completed event, and then return.

function CustomerClass:Walk()
	print(self.Name, " walks")
	local goal = {}
	goal.Position = Vector3.new(-10.5, 0.5, 0.5)
	
	local tween = TweenService:Create(self.Model, tweenInfo, goal)
	tween:Play()
	
	tween.Completed:Wait(function() -- use Wait() instead of Connect()
		print(self.Name, "arrived")
		return(whatever you want here) -- return something to the original function
	end)
end

More about :Wait() here: RBXScriptSignal | Documentation - Roblox Creator Hub

1 Like

It worked, but why did :Wait() helped?

:Wait() is exactly like :Connect(), except that it ‘yields’ the thread until the event (here, the event is .Completed) fires. Instead of just connecting it when the .Completed event fires, it quite literally waits until the event fires, THEN runs the code.

1 Like

Wrong for 100%
Wait method yields the current thread untill event is fired; Connect connects a function to a task scheduler of a connection.
That not even remotely close.

But what’s the right way to do it?

If you want to yield the script untill connection is fired you could do

tween.Completed:Wait()
--Code here

But what if i want to know when this method is complete and detect it on server script?

You can check a read only property of a tween “PlaybackState”

if tween.PlaybackState==Enum.PlaybackState.Completed then 
--Tween is completed
end

But Walk method is on module script and i use OOP,
let’s say when tween is over it will print “tween is completed”, so when i use this method on server script it will just print it but how will server know that tween was completed and not just the function

local SimpleCustomer = require(game.ReplicatedStorage.SimpleCustomer)
local newCustomer = SimpleCustomer.new("Челик", workspace.Customer)

newCustomer:Walk()

“erm actually its metatable OOP :nerd_face::point_up: GET OU-”
i already told you that.

1 Like

:thinking::thinking::thinking::thinking::thinking::thinking::thinking::thinking::thinking::thinking::thinking:
hmmmmmmmmmmmmmmmm
yep thats will do

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