Got attempt to index nil with WaitForChild

I got a module script for my tower shop and i added a new item and wanted to use the value i created inside the player and got attempt to index nil with WaitForChild. Some insight would be great. but heres my script

local Players = game:GetService("Players")
local player = Players.LocalPlayer


local TowerShop = {
["Bronze Robloxian"] = {
	["Name"] = "Bronze Robloxian",
	["ImageAsset"] = "rbxassetid://11624666096" ,
	["Price"] = 800 ,
	["Description"] = "Bronze Robloxian will buff the damage of towers around it. When upgraded to gold damage is fully doubled",
	["Event"] = true, 
	},
["Blue Robloxian"] = {
		["Name"] = "Test",
		["ImageAsset"] = "rbxassetid://" ,
		["Price"] = 0,
		["Description"] = "Desc",
		["Event"] = player:WaitForChild("Even").Value,
   }
}
return TowerShop
3 Likes

What sort of script is requiring the module?

2 Likes

Where are you trying to use this Value?

["Event"] = player:WaitForChild("Even").Value,
2 Likes

If you are requiring the module in a server script, Players.LocalPlayer will be nil.

Which is what PoppyandNeivaarecute was going to say I think.

2 Likes

The error “attempt to index nil with WaitForChild” occurs when you try to access a property or method of an object that is nil (meaning it does not exist). In this case, the object is the result of calling WaitForChild() on the player object, and the property you are trying to access is Value .

To fix the error, you can either check that the object you are trying to access exists before trying to access it, or you can use a different method to access the value that you want.

A way to do this is FindFirstChild() method instead of WaitForChild() , and check for the existence of the child before trying to access its value. Alternatively, you can use the try() function to wrap the WaitForChild() call and catch any errors that may occur.

2 Likes
Players.LocalPlayer:WaitForChild("Even").Value
--                /\                   /\
-- the error happens here              |
-- not here --------------------------/

Players:FindFirstChild("LocalPlayer") doesn’t work.

2 Likes
local player = Players.LocalPlayer

You cannot get local player in a module script, only from a local script`

3 Likes

You are right that it is not possible to access the LocalPlayer property from a module script.

You will need to pass it as an argument to the module script. For example, he could modify his module script to accept a player argument like this:

local TowerShop = {
	["Bronze Robloxian"] = {
		["Name"] = "Bronze Robloxian",
		["ImageAsset"] = "rbxassetid://11624666096" ,
		["Price"] = 800 ,
		["Description"] = "Bronze Robloxian will buff the damage of towers around it. When upgraded to gold damage is fully doubled",
		["Event"] = true, 
	},
	["Blue Robloxian"] = {
			["Name"] = "Test",
			["ImageAsset"] = "rbxassetid://" ,
			["Price"] = 0,
			["Description"] = "Desc",
			["Event"] = player:WaitForChild("Even").Value,
	   }
	}
	return TowerShop

Then, in his local script, he could pass the LocalPlayer object as an argument when calling the module script like this:

local player = game.Players.LocalPlayer
local TowerShop = require(moduleScript)(player)
2 Likes

You meant to say:
You can’t get the local player on the server, only on the client.
ModuleScripts have access to LocalPlayer as long as it’s being run from a local script.

4 Likes

Thanks for all the help. I ended up getting it to work. I just did it differently but got it working perfectly. Created a folder in replicated storage same name as the player and then in my local script i had it only show the towers that were in said players folder. Works out flawlessly

1 Like