Attempt to call a nil value

So I was programming a spectate system that would spectate players that are only in the round. I decided to use one of my module tables. The first thing I did was make a module function that allowed me to get all the players from that table. Then use that function in the spectate script. But for some reason when I get the children of the players it shows an error.

The function that gets all the children in the table:

function moduleFunction.PlayersAlive()
	playersInRound:GetChildren()
end

The spectate script:

local debounce = false
local toggle = script.Parent.Parent.Buttons.SpectateButton.ImageButtonRoundify12px
local frame = script.Parent
local otherFrame = script.Parent.Parent.InventoryFrame
local otherFrame2 = script.Parent.Parent.SettingsFrame
local previous = frame.PrevButton.Button
local next = frame.NextButton.Button
local status = frame.RoundifyImage12px.Status
local camera = game.Workspace.CurrentCamera
local num = 1
local gameModule = require(game.ReplicatedStorage.StatusModule)


status.Text = game.Players.LocalPlayer.Name

toggle.MouseButton1Click:connect(function() 
	local players = gameModule.PlayersAlive()
	if #players < 1 then return end
	otherFrame.Visible = false
	otherFrame.Position = UDim2.new(0.499, 0, 0.369, 0)
	otherFrame2.Visible = false
	otherFrame2.Position = UDim2.new(0.499, 0, 0.369, 0)
	if frame.Visible == false then
		debounce = true
		frame.Visible = true
		frame:TweenPosition(UDim2.new(0.218, 0, 0.646, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.0625, true)
		debounce = false
	elseif frame.Visible == true then
		debounce = true
		frame.Visible = false
		frame.Position = UDim2.new(0.218, 0, 0.716, 0)
		debounce = false
	end	
end)

previous.MouseButton1Click:Connect(function()
	local players = gameModule.PlayersAlive()
	local max = #players
	num = num - 1
	if num < 1 then
		num = max
	end
	local player = players[num]
	camera.CameraSubject = player.Character.Humanoid
	status.Text = player.Name
end)

next.MouseButton1Click:Connect(function()
	local players = gameModule.PlayersAlive()
	local max = #players
	num = num + 1
	if num > max then
		num = 1
	end
	local player = players[num]
	camera.CameraSubject = player.Character.Humanoid
	status.Text = player.Name
end)

frame.Changed:Connect(function()
	if not frame.Visible then
		camera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
		status.Text = game.Players.LocalPlayer.Name
	end
end)

I tried searching this up but nothing came to be related. If someone could help, I would appreciate it.

1 Like

Can you show me the whole script that gets all the children in the table please

Can you also send what line that this error is occurring on (as well as what is on that line)?

Its a module function
Here it is:

local moduleFunction = {}
local playersInRound = {}
local connections = {}

local playerInfo = { 
	AFK = false; 
	InRound = false 
}

function moduleFunction.ReturnPlayersInfo()
	return playerInfo
end
---------------------------------------------------------------------------------------------------------------------------
-- // Game Functions \\ --
---------------------------------------------------------------------------------------------------------------------------
function moduleFunction.AddValue()
	local Players = game:GetService("Players")
	Players.PlayerAdded:Connect(function(player)

		local isPlaying  = Instance.new("BoolValue")
		isPlaying.Name = "IsPlaying" 
		isPlaying.Parent = player 

	end)
end

function moduleFunction.AddPlayer(player)
	if player:IsA("Player") then
		if playerInfo.AFK == false then
			playerInfo.InRound = true
			print(player)
			table.insert(playersInRound, player) 
			connections[player.Name] = player.Character.Humanoid.Died:Connect(function()
				table.remove(playersInRound, table.find(playersInRound, player))
			end)
		elseif playerInfo.AFK == true then
			warn("Player is afk. Was not added to the list")
			playerInfo.InRound = false
		end	
	end
end

function moduleFunction.PlayerLeft()
	connections["Removing"] = game.Players.PlayerRemoving:Connect(function(player) 
		local playerLeft = table.find(playersInRound, player)
		if playerLeft then
			table.remove(playersInRound, playerLeft)
			playerInfo.InRound = false
		end
	end)
end

function moduleFunction.PlayersAlive()
	playersInRound:GetChildren()
end

function moduleFunction.CheckWinner(parent)
	if #playersInRound > 1 then
		while #playersInRound >= 2 do
			parent.Value = "Game in progress"
			wait()
			if #playersInRound == 1 then
				local player = playersInRound[1]
				print("The Winner is: " .. player.Name)
				parent.Value = player.Name .. " has won this round!"
				wait(5)
			elseif #playersInRound == 0 then
				print("There was not a single winner.")
				parent.Value = "No one won this round"
				wait(5)
			end
		end
	end
end

function moduleFunction.RemovePlayer()
	for _, connection in pairs(connections) do 
		connection:Disconnect()
		wait()
	end

	for _, player in pairs(playersInRound)do
		player:LoadCharacter()
		wait()
	end	
	
	playerInfo.InRound = false
	playersInRound = {}
end 

return moduleFunction

Note: Ignore the player info, I did NOT use this table AT ALL. It was just a test.

image

There.

Could you send what’s on line 58 too. It’s going to take me a while to count the lines and get there.

I think the reason is that it’s expecting an Instance | Roblox Creator Documentation when it’s looking through.

Also have you assigned the table to something?

Oh whoops wrong image, sorry this one is the image:

image

and the code:

1 Like

Yes I assigned it to the players in the round.

If you take a glance at the module you will see.

Ok thank you. This error means that GetChlidren is not a valid member of playersInRound and you’re trying to call it as if it is a function. I believe this is most likely because it is not an instance, but rather a table of sort. (just as @exp_lol123 said)

So how would I be able to get all the players in a table and use it in another script?

playersInRound is most likely just an array of the players in the round, so there is no need to call :GetChildren(). You can instead just do return playersInRound on line 59.

Ok. But is there anyway I could get the players in the table and use it in another script?

Maybe use instance.new to make string values every time there’s a player in a round and maybe use that

Yes, if you call moduleFunction.PlayersAlive() from another script (assuming the script that this function is in is a ModuleScript and you are requiring that module).

put it in a modulescript and you can use it all across your scripts. by requiring it again.

Yes, I did all of that, so your saying I should just put return playersInRound in that function???

Its for a spectate system so I can only spectate players in a round

Yeah, I think that would be the best option.

maybe you could do this
local Value = Instance.new(“StringValue”)
Value.Parent = script
local char = game.Players.LocalPlayer.Character
Value.Name = char.Name – you can ignore this –
Value.Value = char.UserId
you could do something like that