How do I use module scripts correctly?

How do I use module scripts correctly?

So what I want to do and can’t figure out how to do is after using a module script function I want my local script to wait until the whole function is done.

Module script:

module.NightToDay = function(timeSet)
	local info = TweenInfo.new(timeSet)
	ts:Create(lighting,info,{ClockTime = 8,Brightness = 3}):Play()
	local last = ts:Create(atmo,info,{Density = .3})
	last:Play()
	last.Completed:Connect(function()
		return true
	end)
end

local script:

   module.NightToDay(3.5) -- does the night and day cycle
		print("finsihed night to day cycle")
		wait(1)
		module.CamTween(cam,tweenCam,2)
end

What am I doing wrong?

2 Likes

you can do something like this:

module:

module.NightToDay = function(timeSet, Func)
	local info = TweenInfo.new(timeSet)
	ts:Create(lighting,info,{ClockTime = 8,Brightness = 3}):Play()
	local last = ts:Create(atmo,info,{Density = .3})
	last:Play()
	last.Completed:Connect(function()
		Func(true)
	end)
end

local:

module.NightToDay(3.5, function(Completed)
	print("tween completed or whatever")
end)

Use the Wait method of the signal Completed to wait the end of it

last.Completed:Wait()

Also you can’t return true in this case, Completed only return the connection because it’s a signal. If you want to know if the tween have successfully completed use PlaybackState and return the result at the end of your function.

Your ModuleScript:

module.NightToDay = function(timeSet)
	local info = TweenInfo.new(timeSet)
	ts:Create(lighting,info,{ClockTime = 8,Brightness = 3}):Play()
	local last = ts:Create(atmo,info,{Density = .3})
	last:Play()
	last.Completed:Wait()
	return (last.PlaybackState == Enum.PlaybackState.Completed)
end

Your LocalScript:

local success = module.NightToDay(3.5) -- does the night and day cycle
print("Finsihed night to day cycle. Success:", success)
task.wait(1)
module.CamTween(cam,tweenCam,2)
1 Like

The print “finished night to day cycle” is not anyhow correlated to the module script, it just prints out after the module function is done because of how threads work. If you want to print the message after the function is finished then it’s better to add it at the end of your module function.

module.NightToDay = function(timeSet)
	local info = TweenInfo.new(timeSet)
	ts:Create(lighting,info,{ClockTime = 8,Brightness = 3}):Play()
	local last = ts:Create(atmo,info,{Density = .3})
	last:Play()
	last.Completed:Connect(function()
        print("finsihed night to day cycle")
		wait(1)
		module.CamTween(cam,tweenCam,2)
		return true
	end)
end

Btw, a preference choice but you can add functions in a module by doing:

function module.NightToDay(timeSet)
...
end)
1 Like

why is :Wait() better than :Connect()?

In your situation, you don’t need to connect a function to Completed, you just want to wait for the tween to finish before continuing. Wait is designed for this purpose, it pauses the thread until the signal is called. You would use Connect in other contexts, for example, if you don’t want to pause the thread and want to perform an action when the signal is called.

1 Like

I have one more question. If you call a module function in a local script, will it pass by the line you called it right away or wait until the function is finished even if there is no return in the end of the function?

If we take your code as an example, even if the return statement is absent at the end of the function, the LocalScript thread will wait for the called function to complete before continuing. You can desynchronize this function by using task.spawn as follows:

task.spawn(function()
	module.NightToDay(3.5) -- does the night and day cycle
	print("Finsihed night to day cycle. Success:")
	task.wait(1)
	module.CamTween(cam,tweenCam,2)
end)

print("Hello world!")

In this situation, everything within the task.spawn function will be desynchronized from the main thread, and “Hello World!” will be printed before the ending of your tween.

If you have any questions, don’t hesitate to ask me again!

1 Like

So I don’t really need that return in the module script and the code would’ve worked fine?

Yea exactly you don’t need to return the result of the playbackState, it’s just an exemple to show you this concept!

1 Like

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