I can't change index value in my module

I want to change table in module , but it keeps not changing.

--Script
local Module = game.ServerScriptService.Script:WaitForChild("PlayersModule")
game.Players.PlayerAdded:Connect(function(plr)
	local Module = require(Module)
	local AOP = game:GetService("Players"):GetChildren()
	if #AOP == 1 then
		Module:AddPlayer(plr.Name,"Host")  
	else
		Module:AddPlayer(plr.Name,"Player") 
	end
end)
game.Players.PlayerRemoving:Connect(function(plr)
	local Module = require(Module)
	local AOP = game:GetService("Players"):GetChildren()
	local IsHost = Module[plr.Name]
	if IsHost == "Host" and #AOP > 0 then
		local PlrList = Module:RemovePlayer(plr.Name) 
		local Newhost = nil
		pcall(function()
			Newhost = table.find(PlrList[AOP[math.random(#AOP)].Name])  			
		end)
		print(Newhost)
		Module:UpdatePlayer(Newhost,"Host") 
	end
end)
--Module
local PlrList = {
	
}
function PlrList:AddPlayer (Index,Value)
	PlrList[Index] = Value
end 
function PlrList:RemovePlayer (Index)
	local function RefreshTable (tbl)
		local newTbl = {}
		for i,v in pairs(tbl) do
			if v then
				newTbl[i] = v
			end
		end
		return newTbl
	end
	PlrList[Index] = nil
	PlrList = RefreshTable(PlrList)
	return PlrList
end
function PlrList:UpdatePlayer (index,value)
	PlrList[index] = value
end
return PlrList

I think you’re supposed to print PlrList.

i did and it was empty , I saw only functions

Oh, I think I know why now, you’re using PlrList to store both the functions and the players. You should create a separate table inside the modules table that stores the player’s.

the problem is i cannot insert anything into that table , and it’s not functions with player names bc i can’t remove that later

That’s because you already returned the table. Which means you can not retrieve newly added keys from the table.

As others have said above, it already returned the table, I think this is a way to fix it:

--Script

--Variables
local HostModule = require(game.ServerScriptService.Script:WaitForChild("PlayersModule"))

--Example usage: HostModule:IsHost(Player) or HostModule:GetHost()

--Module

--Variables
local Module		=	{}
local Host			=	nil
local PlayerService =	game:GetService("Players")

--Function to bind to player adding
PlayerService.PlayerAdded:Connect(function(Player)
	--If there is no host, find a host. This function will probably only be required at server startup as a server without any users will possibly shutdown
	if not Host then
		Host = Player
		print("New Host:", Player.Name)
	end
end)

--Function to bind to player removal
PlayerService.PlayerRemoving:Connect(function(Player)
	
	--If the player is the host and they are not the only one in the session, find a new host
	local Players	=	PlayerService:GetPlayers()
	if Player == Host and #Players > 1 then
		
		--Remove the leaving player from the list
		table.remove(
			Players,
			table.find(
				Players,
				Player
			)
		)
		
		--Pick a random player to be the host, print their name and add it to the list
		Host = Players[math.random(#Players)]
		print("New Host:", Host.Name)
		
	elseif Player == Host then
		--Usually the server will close itself with no users in it but this is just a quick fix just in case the server continues to run. I am not sure how roblox is handling this internally. So it can't hurt to add this.
		Host = nil
		print("New Host: Nobody")
	end
end)

--Function to check if a player is the host
function Module:IsHost(Player)
	return Player == Host
end

--Function to get the host
function Module:GetHost()
	return  Host
end

return Module

This is a simple way and I think it will work, not tested though.
You don’t need to mark players as player if they’re not the host either.

HostModule:IsHost() To use this, just run HostModule:IsHost(Player) to check if the user is the host or not, if they’re the host it’ll return true, if not it will return false.

HostModule:GetHost() To use this, just run HostModule:GetHost() to receive the player instance of the current host. It will return a player instance or nil (if there is no current host).

This way the script will also stay clean and the module handles itself alone. I tried adding some notes to make things maybe a bit more clear idk

Edit:
I can’t guarantee it will work, but I could help if you run into any issues or if it doesn’t work.

1 Like