Module Scripts Acting as Objects

Can anyone explain to me why I am getting this error?

ReplicatedStorage.Modules.ClientScreen:26: attempt to index nil with 'world'

My module is as follows:

local clientScreen = {
	screens = {}
}

function clientScreen.setCharacterFrames(player, characterFrames)
	clientScreen.screens[player].characterFrames = characterFrames
end

function clientScreen.getCharacterFrames(player)
	return clientScreen.screens[player].characterFrames
end

function clientScreen.setCharacterColor(player, characterColor)
	clientScreen.screens[player].characterColor = characterColor
end

function clientScreen.getCharacterColor(player)
	return clientScreen.screens[player].characterColor
end

function clientScreen.setWorld(player, world)
	clientScreen.screens[player].world = world
end

function clientScreen.getWorld(player)
	return clientScreen.screens[player].world
end

function clientScreen.setGround(player, ground)
	clientScreen.screens[player].ground = ground
end

function clientScreen.getGround(player)
	return clientScreen.screens[player].ground
end

function clientScreen.setBarriers(player, barriers)
	clientScreen.screens[player].barriers = barriers
end

function clientScreen.getBarriers(player)
	return clientScreen.screens[player].barriers
end

function clientScreen.setCharacters(player, characters)
	clientScreen.screens[player].characters = characters
end

function clientScreen.getCharacters(player)
	return clientScreen.screens[player].characters	
end

function clientScreen.setCharacter(player, character)
	clientScreen.screens[player].character = character
end

function clientScreen.getCharacter(player)
	return clientScreen.screens[player].character	
end

return clientScreen

And the code calling it is:

-- Give the player a server copy of the Gui in ServerStorage
	clientScreen.setWorld(player, serverScreen.getWorld():Clone())
	clientScreen.setGround(player, clientScreen.getWorld(player).Environment.Ground)
	clientScreen.setBarriers(player, clientScreen.getWorld(player).Environment.Barriers)
	clientScreen.getWorld(player).Parent = player.PlayerGui

clientScreen.screens[player] is not being set as a table so it is equal to nil, so you should have a PlayerAdded event along with a PlayerRemoving event to add/remove the player table when they are in the game. clientScreen.screens[player] is nil so when you try to set the world it is indexing nil.

It is in the player added event. That’s what I thought at first.

Players.PlayerAdded:Connect(function(player)
	-- Load the player's data
	loadData(player)
	
	-- If a game has already started, make the player joined a ghost
	if gameData.getIsGameInProgress() then
		serverPlayerData.setIsAlive(player, false)
	end
	
	-- Give the player a server copy of the Gui in ServerStorage
	clientScreen.setWorld(player, serverScreen.getWorld():Clone())
	clientScreen.setGround(player, clientScreen.getWorld(player).Environment.Ground)
	clientScreen.setBarriers(player, clientScreen.getWorld(player).Environment.Barriers)
	clientScreen.getWorld(player).Parent = player.PlayerGui
	
	-- Trigger UI Customizations
	events.disableTopBarEvent:FireClient(player)
	
	-- Add the player that joined to the server's world
	events.createPlayerEvent:Fire(player)

end)

Is a form of clientScreen.screens[player] inside the PlayerAdded event and what does LoadData do?

loadData(player) is just retrieving leaderstat values. I’m unsure of what you mean by a form of the clientScreen.screens[player] inside the PlayerAdded event

I mean is clientScreen.screens[player] being set to a table or is it left empty when the player joins, because it looks like it isn’t being set.

You could do clientScreen.screens[player] = {} right before the line clientScreen.setWorld(player, serverScreen.getWorld():Clone())

1 Like

I just did that no improvement :frowning:

Oh I didnt do that exactly. One minute.

You are a lifesaver. THANK YOU