How to get the player in a module script

Hello! I’m trying to make it so that my remoteEvent fires when the waitTime == 7. However I’m not sure how to get the player and then pass it as a paramater. I need the player in-order to get it’s Gui. Any solutions? (without using player added)

Context: When waitTime == 7, fires event which will display the role frame.

--MODULE SCRIPT--
local gameStatusModule = {}

local Players = game:GetService("Players")

local intermissionEvent = game.ReplicatedStorage.RemoteEvents:WaitForChild("IntermissionEvent")
local gameEvent = game.ReplicatedStorage.RemoteEvents:WaitForChild("GameEvent")
local postponeEvent = game.ReplicatedStorage.RemoteEvents:WaitForChild("PostponeEvent")
local roleEvent = game.ReplicatedStorage.RemoteEvents:WaitForChild("RoleEvent")

gameStatusModule.intermission = function(message, timer)
	local waitTime = 0
	local timer = 15
	while waitTime < 16 do
		intermissionEvent:FireAllClients(message, timer)
		warn(waitTime)
		waitTime += 1
		timer -= 1
		wait(1)
		if waitTime == 7 then
			warn("displaying role")
			roleEvent:FireClient() -- how do I pass a player in this paramater??
		end
		if #Players:GetPlayers() < 2 then
			warn("intermission stopped: not enough players!")
			break
		end
	end
	return true -- when waitTime hits 16, the function will return true when finished
end

return gameStatusModule

--CLIENTSCRIPT--
local roleEvent = game.ReplicatedStorage.RemoteEvents:WaitForChild("RoleEvent")

roleEvent.OnClientEvent:Connect(function()
	local playerGui = player:WaitForChild("PlayerGui")
	local roundGui = playerGui:WaitForChild("RoundGui")
	
	roundGui.RoleFrame.Visible = true
end)
1 Like

ModuleScripts work for both the Server and Client, You could do this:

module.Player = game.Players.LocalPlayer -- Player Variable (Client Only)

function module.thing(p: Player?) -- Player Argument (Server and Client)

oh okay, but when I run the code here in the main script, how do I pass the paramater on the server since im calling the module on the server.

--SERVER--
local gameStatusModule = require(game.ReplicatedStorage.GameStatusModule)
local roleModule = require(game.ReplicatedStorage.RoleModule)

local Players = game:GetService("Players")

local inRound = false

local enoughPlayers = false

local isEnoughPlayers = gameStatusModule.notEnoughPlayers

while task.wait() do 
	if #Players:GetPlayers() < 2 then
		warn("not enough players")
		gameStatusModule.notEnoughPlayers("Need at least 1 more player to start!")
	elseif #Players:GetPlayers() >= 2 and inRound == false then
		warn("run intermission")
		gameStatusModule.intermission("Intermission: ")
		roleModule.roleSelector() -- how do I get player here?
		local isIntermissionFinished = gameStatusModule.intermission
		if isIntermissionFinished then
			inRound = true
			warn("run game")
			gameStatusModule.startGame("Drawer has ")
			local isGameFinished = gameStatusModule.startGame
			if isGameFinished then
				inRound = false
				warn("game ended")
			end
		end
	end
end


--MODULE--
local roleModule = {}

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")

local player = Players.LocalPlayer

local roleEvent = game.ReplicatedStorage.RemoteEvents.RoleEvent

roleModule.roleSelector = function(player)
	local playerGui = player:WaitForChild("PlayerGui")
	local RoundGui = playerGui:WaitForChild("RoundGui")
	
	local RoleFrame = RoundGui.RoleFrame
	warn("display roles")
	roleEvent.OnClientEvent(player)
end

return roleModule
--ONCLIENT--
local roleEvent = game.ReplicatedStorage.RemoteEvents:WaitForChild("RoleEvent")

roleEvent.OnClientEvent:Connect(function(player)
	local roundGui = player:WaitForChild("PlayerGui"):WaitForChild("RoundGui")
	
	roundGui.RoleFrame.Visible = true
end)

What you can do is to pass the player name as a parameter.
Then on the client side you can use Players:GetPlayers() to get a table with all the players.
Then iterate through the list to find the player with the name you passed as a parameter.

1 Like