Script doesn't make screengui appear if there are 2 players

You can write your topic however you want, but you need to answer these questions:
I’m trying to make a talk system where it chooses a random player to select what the bot says.

But when I do 2 players but it shows nothing, can you help?
ScreenShots:


yeah

Script:

local function Talk()
	local Players
	for i, v in pairs(game.Players:GetPlayers()) do
		Players = v
	end
	if Players ~= nil then
		local getplayers = game.Players:GetPlayers()
		local random = math.random(1, #getplayers)
		if #getplayers > 0 then
			local chosenPlayer = getplayers[random]
			if chosenPlayer then
				chosenPlayer.PlayerGui.make.Enabled = true
				local random2 = math.random(1, #workspace.Chars:GetChildren())
				local chosenChar = workspace.Chars:GetChildren()[random2]
				chosenPlayer.PlayerGui.make.Frame.TextLabel.Text = "Make ".. chosenChar.Name .." Talk!"
				game.ReplicatedStorage.Talk.OnServerEvent:Connect(function(player, input)
					if player == chosenPlayer and input ~= nil then
						player.PlayerGui.make.Enabled = false
						Players.PlayerGui.Talk.Enabled = true
						player.PlayerGui.Talk.Frame:WaitForChild("Name").Text = chosenChar.Name
						player.PlayerGui.Talk.Frame.TextLabel.Text = input
						wait(3)
						Players.PlayerGui.Talk.Enabled = false
						Talk()
					end
				end)
			end
		end
	end
end
wait(3)
Talk()

(don’t mind the name of the screenshots)

In the server, there are no errors, so can you help me? Thank you.

For the script u only made it so it will run if

	local Players
	for i, v in pairs(game.Players:GetPlayers()) do
		Players = v
	end

doesnt work properly/ returns nil but I think in this case it is working and since it is not nil its not running the others so
try this:

local function Talk()
	local Players
	for i, v in pairs(game.Players:GetPlayers()) do
		Players = v
	end
	if Players ~= nil then
		local getplayers = game.Players:GetPlayers()
		local random = math.random(1, #getplayers)
		if #getplayers > 0 then
			local chosenPlayer = getplayers[random]
			if chosenPlayer then
				chosenPlayer.PlayerGui.make.Enabled = true
				local random2 = math.random(1, #workspace.Chars:GetChildren())
				local chosenChar = workspace.Chars:GetChildren()[random2]
				chosenPlayer.PlayerGui.make.Frame.TextLabel.Text = "Make ".. chosenChar.Name .." Talk!"
				game.ReplicatedStorage.Talk.OnServerEvent:Connect(function(player, input)
					if player == chosenPlayer and input ~= nil then
						player.PlayerGui.make.Enabled = false
						Players.PlayerGui.Talk.Enabled = true
						player.PlayerGui.Talk.Frame:WaitForChild("Name").Text = chosenChar.Name
						player.PlayerGui.Talk.Frame.TextLabel.Text = input
						wait(3)
						Players.PlayerGui.Talk.Enabled = false
						Talk()
					end
				end)
			end
		end
	else
		local getplayers = Players
		local random = math.random(1, #getplayers)
		if #getplayers > 0 then
			local chosenPlayer = getplayers[random]
			if chosenPlayer then
				chosenPlayer.PlayerGui.make.Enabled = true
				local random2 = math.random(1, #workspace.Chars:GetChildren())
				local chosenChar = workspace.Chars:GetChildren()[random2]
				chosenPlayer.PlayerGui.make.Frame.TextLabel.Text = "Make ".. chosenChar.Name .." Talk!"
				game.ReplicatedStorage.Talk.OnServerEvent:Connect(function(player, input)
					if player == chosenPlayer and input ~= nil then
						player.PlayerGui.make.Enabled = false
						Players.PlayerGui.Talk.Enabled = true
						player.PlayerGui.Talk.Frame:WaitForChild("Name").Text = chosenChar.Name
						player.PlayerGui.Talk.Frame.TextLabel.Text = input
						wait(3)
						Players.PlayerGui.Talk.Enabled = false
						Talk()
					end
				end)
			end
		end
	end
end
wait(3)
Talk()

Okay, thank you. I appreciate your help but it says this: ServerScriptService.Script:31: attempt to get length of a nil value, do you know how to fix this? Edit: i fixed the problem but there is still some bugs like choosing all players and showing the text to 1 person

1 Like

You can just do local Players = game.Players:GetPlayers() instead and do if #Players > 0 then

2 Likes

Why in the first for loop you set the Players to equal only one?

Where? which line in the script?