Need help with ModuleScript

Hi. I’m creating a visual novel from scratch and I have one problem…

I have a function that outputs text, and then the function is only ended when the player clicks a button. I’ve noticed that when a new function is called, the function that is called when the button is pressed is called more and more often.

An example of what I’m talking about:

module.text(RandomDude, "Text")

Pressed

module.text(RandomDude, "Text")

Pressed (x2)

module.text(RandomDude, "Text")

Pressed (x3)

The function is called more each time.
000 print
Devices start to lag when pressed

Code (Only the important lines):

function module.text(char, text)
	if Settings.EventSettings.Launched == false then return false end
	
	local val = false
	
	local function Pressed() print("Pressed") val = true return true end
	
	local function AutoPressed() print("Fire Auto!") task.wait(Settings.MainSettings.AutoDelay) Pressed() return end
	
	local Gui = Settings.MainSettings.Gui
	
	local Button = Gui.VisualNovel.Button
	
	------------------------------------------------------
	--The function of the Gui module is called here
	------------------------------------------------------

	Button.MouseButton1Click:Connect(Pressed)
	
	AutoEvent.Event:Connect(AutoPressed)
	
	if Settings.EventSettings.Auto == true then AutoEvent:Fire() end
	
	repeat wait() until val == true
	
	return true
end

I couldn’t find anything similar on DevForum and Assistant is also powerless.

1 Like

To answer your question about the lag, I think that’s just because of the print statements. They can make your game laggier, they’re useful debugging tools. However, every game gets rid of their print statements once they’re ready because of this reason.

This is happening because each time you press the button, you are connecting a new event to run the AutoPressed function

This is right. instead of using Button.MouseButton1Click:Connect(Pressed),
try using Button.MouseButton1Click:Once(Pressed)

This is the same as connecting but it will only listen for one event instead of every future event

I hate Luau
000 Luau
The function itself is executed once, but print() with the previous functions. I did a stress test and without print() the performance is fine.

Yeah it’s a huge downside to luau, I’m glad that fixed your problem buddy. I guess that’s a lesson to be learnt, print statements are definitely useful pre-release for debugging and what not. But make sure to get rid of all of them once the game is coming out.

1 Like

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