Is not a valid member of Players "Players"

Hi I’m making tic tac toe and it works nicely until you try and play it the second time. It just gives me an error saying 'Is not a valid member of Players “Players”. So like? Blank is not a valid member of Players? This is the line it is saying it on:

if bruh.Value == 2 and game.Players[secondPlayer.Value].PlayerGui.TicTacToe.winValue.Value ~= 1 and game.Players[player.Value].PlayerGui.TicTacToe.winValue.Value ~= 1 then	

And the local secondPlayer and local player is at the top of the script. I tried putting them inside of here but it didn’t help. This is what happens when a player wins:

		local function afterWin()
			local winner = getWinner()
			if gotWinner and winner ~= nil and debounce == false then
				debounce = true

				game.Players[plr.Name].PlayerGui.TicTacToe.winValue.Value = 1
				game.Players[plr.Name].PlayerGui.TicTacToe.Frame.GameFrame.Visible = false
				game.Players[player.Value].PlayerGui.TicTacToe.Frame.GameFrame.Visible = false
				game.Players[plr.Name].PlayerGui.TicTacToe.Frame.Status.Text = winner.." won the game!"
				game.Players[player.Value].PlayerGui.TicTacToe.Frame.Status.Text = winner.." won the game!"
				wait(3)
				game.Players[plr.Name].PlayerGui.TicTacToe.Frame.Visible = false
				game.Players[player.Value].PlayerGui.TicTacToe.Frame.Visible = false
				game.Players[plr.Name].PlayerGui.TicTacToe.Clicked.Value = false
				game.Players[player.Value].PlayerGui.TicTacToe.Clicked.Value = false
				game.Players[plr.Name].PlayerGui.TicTacToe.winValue.Value = 0
				game.Players[player.Value].PlayerGui.TicTacToe.winValue.Value = 0

				script.Parent.Count.Value = nil
				script.Parent.Player.Value = ""
				script.Parent.secondPlayer.Value = ""

				for i,v in pairs(game.Players[plr.Name].PlayerGui.TicTacToe.Frame.GameFrame:GetChildren()) do
					if v:IsA("TextButton") then
						v.Text = ""
					end
				end
				for i,v in pairs(game.Players[player.Value].PlayerGui.TicTacToe.Frame.GameFrame:GetChildren()) do
					if v:IsA("TextButton") then
						v.Text = ""
					end
				end

				local new = game.ServerScriptService.mainTTT:Clone()
				new.Parent = script.Parent
				new.Disabled = false

				script:Destroy()
			end
		end

Can you show the whole script not just one line

I believe that secondPlayer.Value == "", are you sure that the secondPlayer.Value isn’t empty?

script.Parent.MouseClick:Connect(function(plr)		
	local clicked = game.Players[plr.Name].PlayerGui.TicTacToe.Clicked

	if clicked.Value == false and count.Value == 0 or nil then
		clicked.Value = true
		count.Value = count.Value + 1

		game.Players[plr.Name].PlayerGui.TicTacToe.Waiting.Visible = true
		player.Value = plr.Name
	elseif count.Value == 1 and clicked.Value == false and player.Value ~= plr.Name then	
		
		secondPlayer.Value = plr.Name
		
		local function afterWin()

It gets set before.

1 Like
local count = script.Parent.Count
local player = script.Parent.Player
local secondPlayer = script.Parent.secondPlayer
local debounce = false
local gotWinner = false
local board = {
	{"", "", ""},
	{"", "", ""},
	{"", "", ""}
}

function tie()
	game.Players[secondPlayer.Value].PlayerGui.TicTacToe.Frame.Visible = false
	game.Players[player.Value].PlayerGui.TicTacToe.Frame.Visible = false
	game.Players[secondPlayer.Value].PlayerGui.TicTacToe.Clicked.Value = false
	game.Players[player.Value].PlayerGui.TicTacToe.Clicked.Value = false
	game.Players[secondPlayer.Value].PlayerGui.TicTacToe.winValue.Value = 0
	game.Players[player.Value].PlayerGui.TicTacToe.winValue.Value = 0

	script.Parent.Count.Value = nil
	script.Parent.Player.Value = ""
	script.Parent.secondPlayer.Value = ""

	for i,v in pairs(game.Players[plr.Name].PlayerGui.TicTacToe.Frame.GameFrame:GetChildren()) do
		if v:IsA("TextButton") then
			v.Text = ""
		end
	end
	for i,v in pairs(game.Players[player.Value].PlayerGui.TicTacToe.Frame.GameFrame:GetChildren()) do
		if v:IsA("TextButton") then
			v.Text = ""
		end
	end

	local new = game.ServerScriptService.mainTTT:Clone()
	new.Parent = script.Parent
	new.Disabled = false

	script:Destroy()
end

local function getWinner()
	for i = 1, 3 do
		-- Check horizontal
		if board[i][1] ~= "" and board[i][1] == board[i][2] and board[i][2] == board[i][3] and not gotWinner then
			gotWinner = true
			return board[i][1]
		end

		-- Check vertical
		if board[1][i] ~= "" and board[1][i] == board[2][i] and board[2][i] == board[3][i] and not gotWinner then
			gotWinner = true
			return board[1][i]
		end

		-- Check top left to bottom right
		if board[2][2] ~= "" and board[1][1] == board[2][2] and board[2][2] == board[3][3] and not gotWinner then
			gotWinner = true
			return board[2][2]
		end

		-- Check top right to bottom left
		if board[2][2] ~= "" and board[1][3] == board[2][2] and board[2][2] == board[3][1] and not gotWinner then
			gotWinner = true
			return board[2][2]
		end
		
		--Check for tie
		if board[i][1] ~= "" and board[i][2] ~= "" and board[i][3] ~= "" and board[1][i] ~= "" and board[2][i] ~= "" and board[3][i] ~= "" and board[1][1] ~= "" and board[2][2] ~= "" and board[3][3] ~= "" and board[1][3] ~= "" and not gotWinner then
			gotWinner = true
			tie()
		end
	end

	return nil
end

function leftGame()
	game:GetService("Players").PlayerRemoving:Connect(function(plr)
		if plr.Name == player.Value or plr.Name == secondPlayer.Value then
			game.Players[secondPlayer.Value].PlayerGui.TicTacToe.Frame.Visible = false
			game.Players[player.Value].PlayerGui.TicTacToe.Frame.Visible = false
			game.Players[secondPlayer.Value].PlayerGui.TicTacToe.Clicked.Value = false
			game.Players[player.Value].PlayerGui.TicTacToe.Clicked.Value = false
			game.Players[secondPlayer.Value].PlayerGui.TicTacToe.winValue.Value = 0
			game.Players[player.Value].PlayerGui.TicTacToe.winValue.Value = 0
			
			script.Parent.Count.Value = nil
			script.Parent.Player.Value = ""
			script.Parent.secondPlayer.Value = ""

			for i,v in pairs(game.Players[plr.Name].PlayerGui.TicTacToe.Frame.GameFrame:GetChildren()) do
				if v:IsA("TextButton") then
					v.Text = ""
				end
			end
			for i,v in pairs(game.Players[player.Value].PlayerGui.TicTacToe.Frame.GameFrame:GetChildren()) do
				if v:IsA("TextButton") then
					v.Text = ""
				end
			end

			local new = game.ServerScriptService.mainTTT:Clone()
			new.Parent = script.Parent
			new.Disabled = false

			script:Destroy()
		end
	end)
end

script.Parent.MouseClick:Connect(function(plr)		
	local clicked = game.Players[plr.Name].PlayerGui.TicTacToe.Clicked

	if clicked.Value == false and count.Value == 0 or nil then
		clicked.Value = true
		count.Value = count.Value + 1

		game.Players[plr.Name].PlayerGui.TicTacToe.Waiting.Visible = true
		player.Value = plr.Name
	elseif count.Value == 1 and clicked.Value == false and player.Value ~= plr.Name then	
		
		secondPlayer.Value = plr.Name
		
		local function afterWin()
			local winner = getWinner()
			if gotWinner and winner ~= nil and debounce == false then
				debounce = true

				game.Players[plr.Name].PlayerGui.TicTacToe.winValue.Value = 1
				game.Players[plr.Name].PlayerGui.TicTacToe.Frame.GameFrame.Visible = false
				game.Players[player.Value].PlayerGui.TicTacToe.Frame.GameFrame.Visible = false
				game.Players[plr.Name].PlayerGui.TicTacToe.Frame.Status.Text = winner.." won the game!"
				game.Players[player.Value].PlayerGui.TicTacToe.Frame.Status.Text = winner.." won the game!"
				wait(3)
				game.Players[plr.Name].PlayerGui.TicTacToe.Frame.Visible = false
				game.Players[player.Value].PlayerGui.TicTacToe.Frame.Visible = false
				game.Players[plr.Name].PlayerGui.TicTacToe.Clicked.Value = false
				game.Players[player.Value].PlayerGui.TicTacToe.Clicked.Value = false
				game.Players[plr.Name].PlayerGui.TicTacToe.winValue.Value = 0
				game.Players[player.Value].PlayerGui.TicTacToe.winValue.Value = 0

				script.Parent.Count.Value = nil
				script.Parent.Player.Value = ""
				script.Parent.secondPlayer.Value = ""

				for i,v in pairs(game.Players[plr.Name].PlayerGui.TicTacToe.Frame.GameFrame:GetChildren()) do
					if v:IsA("TextButton") then
						v.Text = ""
					end
				end
				for i,v in pairs(game.Players[player.Value].PlayerGui.TicTacToe.Frame.GameFrame:GetChildren()) do
					if v:IsA("TextButton") then
						v.Text = ""
					end
				end

				local new = game.ServerScriptService.mainTTT:Clone()
				new.Parent = script.Parent
				new.Disabled = false

				script:Destroy()
			end
		end
		
		for i,v in pairs(game.Players[plr.Name].PlayerGui.TicTacToe.Frame.GameFrame:GetChildren()) do
			if v:IsA("TextButton") then
				v.Text = ""
			end
		end
		for i,v in pairs(game.Players[player.Value].PlayerGui.TicTacToe.Frame.GameFrame:GetChildren()) do
			if v:IsA("TextButton") then
				v.Text = ""
			end
		end
		
		local firstGui = game.Players[player.Value].PlayerGui.TicTacToe

		local one1 = game.Players[player.Value].PlayerGui.TicTacToe.Frame.GameFrame["1"]
		local two1 = game.Players[player.Value].PlayerGui.TicTacToe.Frame.GameFrame["2"]
		local three1 = game.Players[player.Value].PlayerGui.TicTacToe.Frame.GameFrame["3"]
		local four1 = game.Players[player.Value].PlayerGui.TicTacToe.Frame.GameFrame["4"]
		local five1 = game.Players[player.Value].PlayerGui.TicTacToe.Frame.GameFrame["5"]
		local six1 = game.Players[player.Value].PlayerGui.TicTacToe.Frame.GameFrame["6"]
		local seven1 = game.Players[player.Value].PlayerGui.TicTacToe.Frame.GameFrame["7"]
		local eight1 = game.Players[player.Value].PlayerGui.TicTacToe.Frame.GameFrame["8"]
		local nine1 = game.Players[player.Value].PlayerGui.TicTacToe.Frame.GameFrame["9"]
		
		local secondGui = game.Players[secondPlayer.Value].PlayerGui.TicTacToe

		local one2 = game.Players[secondPlayer.Value].PlayerGui.TicTacToe.Frame.GameFrame["1"]
		local two2 = game.Players[secondPlayer.Value].PlayerGui.TicTacToe.Frame.GameFrame["2"]
		local three2 = game.Players[secondPlayer.Value].PlayerGui.TicTacToe.Frame.GameFrame["3"]
		local four2 = game.Players[secondPlayer.Value].PlayerGui.TicTacToe.Frame.GameFrame["4"]
		local five2 = game.Players[secondPlayer.Value].PlayerGui.TicTacToe.Frame.GameFrame["5"]
		local six2 = game.Players[secondPlayer.Value].PlayerGui.TicTacToe.Frame.GameFrame["6"]
		local seven2 = game.Players[secondPlayer.Value].PlayerGui.TicTacToe.Frame.GameFrame["7"]
		local eight2 = game.Players[secondPlayer.Value].PlayerGui.TicTacToe.Frame.GameFrame["8"]
		local nine2 = game.Players[secondPlayer.Value].PlayerGui.TicTacToe.Frame.GameFrame["9"]

		clicked.Value = true
		count.Value = count.Value + 1

		game.Players[secondPlayer.Value].PlayerGui.TicTacToe.Frame.Status.Text = "You're against: "..player.Value
		game.Players[player.Value].PlayerGui.TicTacToe.Frame.Status.Text = "You're against: "..secondPlayer.Value
		game.Players[secondPlayer.Value].PlayerGui.TicTacToe.Frame.Visible = true
		game.Players[player.Value].PlayerGui.TicTacToe.Frame.Visible = true
		game.Players[secondPlayer.Value].PlayerGui.TicTacToe.Waiting.Visible = false
		game.Players[player.Value].PlayerGui.TicTacToe.Waiting.Visible = false
		wait(3)
		game.Players[secondPlayer.Value].PlayerGui.TicTacToe.Frame.GameFrame.Visible = true
		game.Players[player.Value].PlayerGui.TicTacToe.Frame.GameFrame.Visible = true

		local bruh = script.Parent.bruh
		bruh.Value = 1

		while plr.PlayerGui.TicTacToe.winValue.Value ~= 1 or game.Players[player.Value].PlayerGui.TicTacToe.winValue.Value ~= 1 do wait() 		
			local player = script.Parent.Player
			local secondPlayer = script.Parent.secondPlayer			
			local bruh = script.Parent.bruh
			if bruh.Value == 2 and game.Players[secondPlayer.Value].PlayerGui.TicTacToe.winValue.Value ~= 1 and game.Players[player.Value].PlayerGui.TicTacToe.winValue.Value ~= 1 then			
				one2.Visible = true
				two2.Visible = true
				three2.Visible = true
				four2.Visible = true
				five2.Visible = true
				six2.Visible = true
				seven2.Visible = true
				eight2.Visible = true
				nine2.Visible = true

				one1.Visible = false
				two1.Visible = false
				three1.Visible = false
				four1.Visible = false
				five1.Visible = false
				six1.Visible = false
				seven1.Visible = false
				eight1.Visible = false
				nine1.Visible = false

				game.Players[secondPlayer.Value].PlayerGui.TicTacToe.Frame.Status.Text = "Pick a field!"
				game.Players[player.Value].PlayerGui.TicTacToe.Frame.Status.Text = "The other player is choosing..."

				one2.MouseButton1Click:Connect(function()
					if one2.Text == "" then
						bruh.Value = 1
						
						local fn = 1
						local sn = 1

						board[fn][sn] = secondPlayer.Value

						one1.Text = "X"
						one2.Text = "X"

						afterWin()						
					end
				end)

				two2.MouseButton1Click:Connect(function()
					if two2.Text == "" then
						bruh.Value = 1

						local fn = 1
						local sn = 2

						board[fn][sn] = secondPlayer.Value

						two1.Text = "X"
						two2.Text = "X"

						afterWin()						
					end
				end)

				three2.MouseButton1Click:Connect(function()
					if three2.Text == "" then
						bruh.Value = 1

						local fn = 1
						local sn = 3

						board[fn][sn] = secondPlayer.Value

						three1.Text = "X"
						three2.Text = "X"

						afterWin()						
					end
				end)

				four2.MouseButton1Click:Connect(function()
					if four2.Text == "" then
						bruh.Value = 1

						local fn = 2
						local sn = 1

						board[fn][sn] = secondPlayer.Value

						four1.Text = "X"
						four2.Text = "X"

						afterWin()						
					end
				end)

				five2.MouseButton1Click:Connect(function()
					if five2.Text == "" then
						bruh.Value = 1

						local fn = 2
						local sn = 2

						board[fn][sn] = secondPlayer.Value

						five1.Text = "X"
						five2.Text = "X"

						afterWin()						
					end
				end)

				six2.MouseButton1Click:Connect(function()
					if six2.Text == "" then
						bruh.Value = 1

						local fn = 2
						local sn = 3

						board[fn][sn] = secondPlayer.Value

						six1.Text = "X"
						six2.Text = "X"

						afterWin()						
					end
				end)

				seven2.MouseButton1Click:Connect(function()
					if seven2.Text == "" then
						bruh.Value = 1

						local fn = 3
						local sn = 1

						board[fn][sn] = secondPlayer.Value

						seven1.Text = "X"
						seven2.Text = "X"

						afterWin()						
					end
				end)

				eight2.MouseButton1Click:Connect(function()
					if eight2.Text == "" then
						bruh.Value = 1

						local fn = 3
						local sn = 2

						board[fn][sn] = secondPlayer.Value

						eight1.Text = "X"
						eight2.Text = "X"

						afterWin()						
					end
				end)

				nine2.MouseButton1Click:Connect(function()
					if nine2.Text == "" then
						bruh.Value = 1

						local fn = 3
						local sn = 3

						board[fn][sn] = secondPlayer.Value

						nine1.Text = "X"
						nine2.Text = "X"

						afterWin()						
					end
				end)
			elseif bruh.Value == 1 and plr.Name == secondPlayer.Value and game.Players[secondPlayer.Value].PlayerGui.TicTacToe.winValue.Value ~= 1 and game.Players[player.Value].PlayerGui.TicTacToe.winValue.Value ~= 1 then				
				one2.Visible = false
				two2.Visible = false
				three2.Visible = false
				four2.Visible = false
				five2.Visible = false
				six2.Visible = false
				seven2.Visible = false
				eight2.Visible = false
				nine2.Visible = false

				one1.Visible = true
				two1.Visible = true
				three1.Visible = true
				four1.Visible = true
				five1.Visible = true
				six1.Visible = true
				seven1.Visible = true
				eight1.Visible = true
				nine1.Visible = true

				game.Players[player.Value].PlayerGui.TicTacToe.Frame.Status.Text = "Pick a field!"
				game.Players[plr.Name].PlayerGui.TicTacToe.Frame.Status.Text = "The other player is choosing..."

				one1.MouseButton1Click:Connect(function()
					if one1.Text == "" then
						bruh.Value = 2

						local fn = 1
						local sn = 1

						board[fn][sn] = player.Value

						one1.Text = "O"
						one2.Text = "O"

						afterWin()						
					end
				end)
-- and so on for every button...
			end
		end
	end
end)

game:GetService("ReplicatedStorage"):WaitForChild("TicTacToe").OnServerEvent:Connect(function(playerr)
	local clicked = game.Players[playerr.Name].PlayerGui.TicTacToe.Clicked

	if clicked.Value == true then
		clicked.Value = false
		count.Value = 0
		game.Players[playerr.Name].PlayerGui.TicTacToe.Waiting.Visible = false
		player.Value = ""
	end
end)
1 Like

Doesn’t seem like it from the error, have you tried printing it?

Well it says the error as soon as the script gets cloned and added into the workspace. (Only the cloned one)

Its pretty clear you set second player to “” thats why its erroring

Is pretty bad Your file structure, you shouldnt have values in scripts. This why you are having problems finding what value is set to what by what.

1 Like

Does the error happen when they tie

Is ‘secondPlayer.Value’ or ‘player.Value’ being set to nil (or an empty string) anywhere? Is that is what’s causing your issue.

My mistake was when someone won the game it reset the values before trying to change something using the values.