Attempted to index a value with string.. but why wont it work?

Heya folks! I took a break from scripting for a few months, and came back to it last week. I have scripted this piece of code that assigns players to a table randomly, and the table will be their team.

local storageRep = game.ReplicatedStorage
local status = storageRep:WaitForChild("Status")

game.Players.PlayerAdded:Connect(function(player)
	
	--[[while game.Players.NumPlayers <= 1 do
		status.Value = "PLEASE WAIT FOR ANOTHER PLAYER TO JOIN..."
	end]]
	
	for i = 10,0,-1 do
		status.Value = "Round starting in "..i.." seconds"
		wait(1)
	end
	
	for i,plr in pairs(game.Players:GetPlayers()) do
		local teamSize = 5
		local values = {"BlueTeam", "RedTeam"}
		
		local plrTable = {}
		local RedTeam = {}
		local BlueTeam = {}
		
		local teamName = Instance.new("StringValue")
		teamName.Name = "TeamName"
		teamName.Parent = plr
		
		if teamName.Value == "" then
			table.insert(plrTable,plr)
			local randomChoice = math.random(1,#plrTable)
			randomChoice.TeamName.Value = math.random(1,#values)
			print(randomChoice.TeamName.Value)
			table.insert(randomChoice.TeamName.Value,randomChoice)
			table.remove(plrTable,randomChoice)
		end
	end
end)

However, I keep getting this error:
ServerScriptService.ScriptHolder:30: attempt to index number with ‘TeamName’

Any help? And any improvements to my original code after the fix?

Many thanks!

1 Like

replace this with

local randomChoice = plrTable[math.random(1, #plrTable)]
2 Likes

Also, adding to the above answer, you seem to be inserting a string into a table which isn’t possible as the first argument needs to be a table.

Moreover, this line: table.remove(plrTable,randomChoice) would error, since you’re removing an instance and the second argument requires a number. Change that to: table.remove(plrTable,plrTable[randomChoice])

2 Likes

Ohhhhhhhhhhhhhhhhhhhh
That’s the thing I couldn’t remember!

Never mind, I have used an IF statement. Thanks for all the help guys!