Module Script returning nil

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want to print the player’s table that’s inside of a module script.

  2. What is the issue? Whenever I try to print the module script’s table or values they return errors or nil.

  3. What solutions have you tried so far? I rewrote the script twice and looked for solutions on other posts.

Module Script

local PlayerDict = {}

for i, child in pairs(workspace:GetChildren()) do
	humanoid = child:FindFirstChild("Humanoid")
	npc = child:FindFirstChild("NPC")
	
	if humanoid and npc then
		PlayerDict[child.Name] = {
			Type = "None",
			Enemy = "None",
			Attacking = false,
			Blocking = false,
			Disabled = false,
		}
	end
end

workspace.ChildRemoved:Connect(function(child)
	print((PlayerDict)) -- this prints out the table correctly but the client doesn't
end)

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		PlayerDict[Player.Name] = {
			Test = "worked", 
		}
	end)
end)

return PlayerDict

Local Script

local PlayerDict = require(game.ReplicatedStorage.Modules:WaitForChild("PlayerDict"))
local Events = game.ReplicatedStorage:WaitForChild("Events")
--
local players = game.Players
local lplayer = players.LocalPlayer
local InputService = lplayer:GetMouse()
local P_ = PlayerDict[lplayer.Name]
print(P_) -- prints nil
print(P_.Type) -- prints " attempt to index nil with 'Type' "

This is my first time using a module script and writing a post.
(if you need more info i’ll write it down for you :D)

PlayerDict[lplayer.Name] is nil because you are indexing the player’s name before it has a chance to be added to the table in the CharacterAdded event.

If you want check if it worked or not, you can just add a delay before indexing.

local PlayerDict = require(game.ReplicatedStorage.Modules:WaitForChild("PlayerDict"))

task.wait(5)
local P_ = PlayerDict[lplayer.Name]
print(P_)
print(P_.Type)

Instead of looping through all the children of workspace:

for i, child in pairs(workspace:GetChildren()) do
	humanoid = child:FindFirstChild("Humanoid")
	npc = child:FindFirstChild("NPC")
	
	if humanoid and npc then
		PlayerDict[child.Name] = {
			Type = "None",
			Enemy = "None",
			Attacking = false,
			Blocking = false,
			Disabled = false,
		}
	end
end

use CollectionService and tag all the NPCs in workspace.

1 Like

It still returns the errors but thanks for the tip and taking out time to reply :smiley:

Maybe this is happening because the client can’t detect when itself gets added to the game, it can only detect when others are added afterwards, and the module is being required by the client.
The Roblox documentation on Players.PlayerAdded may explain this further.

If you require the module from the server, then send requests for data from the client via (a) RemoteFunction(s), your issue should be solved. Or, you can just store the LocalPlayer’s information in the module outside of the Players.PlayerAdded event.

Thanks for the solution, it works as intended now :).

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