Unsure of where to put events for module scripts

in my server script i have the PlayerAdded and the PlayerRemoving events. can i put them in my module script instead hence calling the functions also and would it be a recommended practice? i appreciate any feedback on my code as well - thanks guys :slight_smile:

this is my server script:

local plotmanager = require(game.ReplicatedStorage.Scripts.PlotManager)

game.Players.PlayerAdded:Connect(function(player)
	plotmanager:GivePlot(player)
	plotmanager:SetupUnclaimedPlots()
end)

game.Players.PlayerRemoving:Connect(function(player)
	plotmanager:RemovePlot(player)
	plotmanager:SetupUnclaimedPlots()
end)

this is my module script:

local BillboardManager = {}

local plots = workspace.Plots
local player = game.Players.LocalPlayer
local PlotSigns = workspace:FindFirstChild("PlotSigns")

function BillboardManager:UpdateClientPlotName()
	for _, plot in pairs(plots:GetChildren()) do
		if not plot:GetAttribute("owner") then continue end
		if plot:GetAttribute("owner") ~= player.UserId then continue end
		local PlotTag = plot:FindFirstChild("PlotTag")
		if PlotTag and PlotTag:IsA("BillboardGui") then
			local frame = PlotTag:FindFirstChild("Frame")
			local name = frame:FindFirstChild("name")
			local stroke = frame:FindFirstChild("stroke")
			if name then name.Text = "Your Plot" end
			if stroke then stroke.Text = "Your Plot" end
		end
	end
end

function BillboardManager:UpdateBillboards()
	local character = player.Character or player.CharacterAdded:Wait()
	if not character then return end
	local HumanoidRootPart = character:FindFirstChild("HumanoidRootPart")
	if not HumanoidRootPart then return end

	for _, plot in pairs(plots:GetChildren()) do
		if not plot:IsA("Model") then continue end
		if not plot:GetAttribute("owner") then continue end
		if plot:GetAttribute("owner") ~= player.UserId then continue end

		local cframe, size = plot:GetBoundingBox()
		local distance = (HumanoidRootPart.Position - cframe.Position).Magnitude
		local MAX_DISTANCE = (size.X / 2)
		local MIN_DISTANCE = MAX_DISTANCE - 6

		local PlotTag = plot:FindFirstChild("PlotTag")
		if PlotTag and PlotTag:IsA("BillboardGui") then
			local frame = PlotTag:FindFirstChild("Frame")
			local name = frame.name
			local stroke = frame.stroke
			if frame then
				local alpha = math.clamp((distance - MIN_DISTANCE) / (MAX_DISTANCE - MIN_DISTANCE), 0, 1)
				frame.Size = UDim2.new(alpha, 0, alpha, 0)
				--print(
				--	"min:", distance - MIN_DISTANCE,
				--	"max:", MAX_DISTANCE - MIN_DISTANCE,
				--	"alpha", alpha
				--)
			end
		end
	end
end

return BillboardManager

You can put the PlayerAdded and PlayerRemoving in the module and requiring the module would catch all those events. I am not sure if this a recommended practice though.

2 Likes

This is what I do.

Normal script:

-- let the modules load first
local modules = {}
for _, i in ipairs(script:GetDescendants()) do -- modules parented to script
	if i:IsA("ModuleScript") then
		modules[i.Name] = require(i)
	end
end
-- check modules for _init function, then run it
for _, i in pairs(modules) do
	if type(i) == "table" and i._init then
		i._init()
	end
end

Module (let’s call this one PlayerAddedRemoved):

local PlayerAddedRemoved = {}

local Players = game:GetService("Players")

local plotmanager = require(game.ReplicatedStorage.Scripts.PlotManager)

local function onPlayerAdded(player)
    plotmanager:GivePlot(player)
	plotmanager:SetupUnclaimedPlots()
end

local function onPlayerRemoving(player)
	plotmanager:RemovePlot(player)
	plotmanager:SetupUnclaimedPlots()
end

function PlayerAddedRemoved._init()
    Players.PlayerAdded:Connect(onPlayerAdded)
    Players.PlayerRemoving:Connect(onPlayerRemoving)
end

return PlayerAddedRemoved

_init is treated as an initializer function which is called immediately after all modules are required. This way you can ensure that all your modules are ready to be used before they start using other modules. You don’t have to make a separate module for this like I did, but when it comes to PlayerAdded and PlayerRemoved, I would recommend for you to have a module whose sole purpose is to handle those two events because it’s easier to find that way.

1 Like

oh thats a really cool way of doing it actually. i assume for systems relating to the PlayerAdded, PlayerRemoving events u would handle all ur systems in that module?

thanks so much for ur help! big fan of rugby rumble btw - great game. :slight_smile:

1 Like