ServerScriptService.PlayerTeam:14: attempt to call a nil value

image

local Players = game:GetService("Players")
local stages = game.Workspace.Stages:GetChildren()
local playerAdded

local teams = game:GetService("Teams")
local stageCheckTable = teams:GetChildren()

function onCharacterAdded(character)
	for i, v in pairs(stageCheckTable) do
		print (type(v))
		if v == playerAdded.Team then
			local stageGroup = stages:WaitForChild(v)
			local spawnPart = stageGroup:WaitForChild(v)
			wait()
			local spawnPartPosition = spawnPart.Position
			character:moveTo(spawnPartPosition)
		end
	end
end

function onPlayerAdded(player)
	playerAdded = player
	player.Team = game.Teams["Stage1"]
	player.CharacterAdded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)

Currently when I try to get the team which the player is on (Stage1) and compare it to the correspondingly named (Stage1) in order to move the player to that specific part, I get the error (ServerScriptService.PlayerTeam:14: attempt to call a nil value). I assume the issue stems from the fact that v is user data and what I’m trying to compare it to is a string, however I don’t know a method which allows me to convert user data into a string (yes I have tried tostring()). Is there a way to convert user data into a string, or an entirely other method to go about this which I don’t know about. Thank you!

I’d assume this is what you want:

local stageGroup = stages[v.Name]
local spawnPart = stageGroup[v.Name]
1 Like

I still get the same error, is there a way of converting userdata into a string to be used to compare to another string? (I have tried tostring() with no success)

I think your issue here is local stages = game.Workspace.Stages:GetChildren()

local stageGroup = game.Workspace.Stages:WaitForChild(v.Name) should work for you.

You were doing :WaitForChild on a table.