Local script running module script multiple times

I can’t figure out what is going on here, I have a prox. prompt that triggers the minigame that the player wishes to play (mainly gui based). It calls the relevant module script as passed through the remote. However, when the player returns to the same minigame it’s as if the module is duplicated (remotes inside the module are firing multiple times depending on how many times the player has returned to the minigame)

RemoteMinigamer.OnClientEvent:Connect(function(minigame, value)
	if questing == false and minigaming == false and equipped == false then
		minigaming = true
		local minigameModule = require(ReplicatedStorage.Minigames[minigame]:Clone())
		minigameModule(player, value)
		return
	else
		return print ("Player already has a minigame running")
	end
end)
1 Like

you are cloning the module in here

ReplicatedStorage.Minigames[minigame]:Clone()

you should just require it without cloning

Yes, the clone was a “workaround” to try and require a new instance every time, without clone it still runs twice if the player returns

1 Like

That probably happens because of old connection stacking, you should add a connection cleanup logic after player leaves the minigame instead of cloning the module.

Tried that too but the remote event can’t be spontaneously re-connected. It remains disconnected

For some reason I need to write a certain number of characters to bump the post :slight_smile:

Still having issues with this if anyone is willing to lend a hand

Hey! Can you try adding a print statement to ensure it’s not the event firing twice?

Perhaps try adding a debounce to the module itself.

I would also suggest requiring the module outside the event.

Yeah it’s definitely not the event firing twice.

I tried requiring the module outside of the event as well and didn’t change anything. I’m really stuck as to what could be the problem

Can you share the code of the module itself?

I see, can you try adding various print statements at every important part to see where it is actually firing twice? This would just help us narrow down the bugged point.

Just tested, every function is firing twice, to me it seems like the module script is being held in cache and not closed out, so when the module is called again it’s duplicated. I even tried :Destroy() when the return value is fired (since I tried cloning the module in the first require anyway) and it still runs double/triple+

This is quite interesting to say the least.

Maybe just add a debounce to the module?

What use would that do, as soon as the remote even connects again it’ll reverse the debounce

12 hour update, still persisting. Any ideas would be greatly appreciated

What I did notice in Studio (also replicated in live game of course) is that the prints on the 2nd+ times are from the client + studio output

Ok so managed to figure out a solution, using _G you can ensure that only a single connection is made

if not _G.minigameEventConnected then
	_G.minigameEventConnected = true
	RemoteMinigamer.OnClientEvent:Connect(StartMinigame)
end