Modulescript not receiving server event

im trying to have this module script receive the event that is being fired from this local script the module script is not receiving anything, but if i were to put it into a normal script it would work??

btw this my first post so sorry if i have done anything wrong here

   local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local PlayerData = require(ServerScriptService.PlayerData.Manager)
local RebirthConfig = require(ReplicatedStorage.Configs.Rebirths)
local Remotes = ReplicatedStorage.Remotes

local Rebirth = {}

function Rebirth.Rebirth(player: Player, amount: number?)
	amount = if amount then amount else 1
	
	local profile = PlayerData.Profiles[player]
	if not profile then return end
	
	local currentRebirth = profile.Data.Rebirths
	local price = RebirthConfig.CalculatePrice(currentRebirth, amount)
	
	local canAfford = profile.Data.Clicks >= price
	if not canAfford then return end
	
	PlayerData.AdjustRebirths(player, amount)
	PlayerData.AdjustClicks(player, -profile.Data.Clicks)
	PlayerData.AdjustDiamonds(player, 10 * amount)
	return "You rebirthed ".. amount.. " times!"
end

Remotes.RequestRebirth.OnServerEvent:Connect(function(player: Player, rebirth: string)
	print("stupid")
	rebirth = tonumber(rebirth)
	if not rebirth then return end
	Rebirth.Rebirth(player, rebirth)
end)

return Rebirth

im 100% sure theres nothing wrong with the local script that fires the event, as any other normal script would receive it

Correct, you cannot connect events in module scripts. This is because module scripts run only once. You must connect the event in a regular Script or LocalScript, and then require the module script to use its functions from there.

2 Likes

Modules have to always be called by a script or a localscript, meaning it will not run on its own.

In order to make it run like that, you would have to require it by a script first, and make for example

module.init = function() 
   Remotes.RequestRebirth.OnServerEvent:Connect(function(player: Player, rebirth: string)
	print("stupid")
	rebirth = tonumber(rebirth)
	if not rebirth then return end
	Rebirth.Rebirth(player, rebirth)
    end)
end

and then call the init() function by a script, in this case, the connect would run each time it has done the thing yk.

1 Like

You can; you just need to require it first

1 Like

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