Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Can't change Text of TextLabel?

function startgame()
wait()
if game.Players.NumPlayers >= 1 then
	local message = game.StarterGui.ScreenGui.TextLabel
	message.Text = "Getting The Game Ready"
	local model = game.Lighting.Maps
	local map = model:GetChildren()[math.random(1, #model:GetChildren())]
	startgame()
	end
end
startgame()

The Output tells me nothing and it does not change the Text of the TextLabel.

Hope you can help. Thanks!

3 answers

✓ Accepted answer

So the Main Reason this won't work is because Starter Gui. Go to the link and read the top line for a simple explaination of why you're script doesn't work.

Now let's get down to business and write some code!

local gameRunning = false -- Variable to see if game is running

function sendMessage(message) -- Function for sending everyone a message in the textlabel!
	for _, player in pairs(game.Players:GetPlayers()) do -- Cycles through all the players in game!
		local label= player.PlayerGui.ScreenGui.TextLabel
		label.Text = message -- Sets the message
	end
end

function startGame()
	if #game.Players:GetPlayers() >= 1 then -- Checks if the player count is greater than 1
		if gameRunning then return end
		gameRunning = true
		sendMessage("Getting The Game Ready!")
		local model = game.Lighting.Maps -- Gets all the maps (I recommend switching to ServerStorage!)
		local map = model:GetChildren()[math.random(1, #model:GetChildren())] -- Randomly selects a map!
		wait(3)
		sendMessage("Map is: " .. map.Name)
		startGame() -- I don't get why you call this again, but I'll leave this here for you.
	end
end

game.Players.PlayerAdded:connect(startGame) -- Calls function "startGame" when ever a player joins!

So, that's the code, hopefully that will help! Please read the comments. Don't just copy and paste btw! My code isn't always written correctly!

2 comments

0

That makes me confused

0

Basically the other function 'SendMessage' is just a way of writing less code, so instead of having a lot of code that is repeated, I just used this

StarterGui is used to give GUIs to the player when the player either enters the game or spawns or respawns. Therefore, the only way you would have seen this take effect is if you resetted your character.

Instead, you should use PlayerGui which is hosted within the player. Anything you change in a player's PlayerGui will automatically be seen by said player.

So to apply this, we will go through all the players and change the text in their textlabel that is located in their PlayerGui. To do this, we will use a for loop and the GetPlayers method of game.Players.

function startgame()
    	    wait()
	-- no need for checking if there is one player in the game
	-- if there was less then 1 player, the game would not even be active
	    local message = game.StarterGui.ScreenGui.TextLabel
	    message.Text = "Getting The Game Ready"
	for i,v in pairs(game.Players:GetPlayers()) do
		local pmessage = game.PlayerGui.ScreenGui.TextLabel
		pmessage.Text = "Getting The Game Ready"
	end
	    local model = game.Lighting.Maps
	    local map = model:GetChildren()[math.random(1, #model:GetChildren())]
	    wait(1)
	for i,v in pairs(game.Players:GetPlayers()) do
		local pmessage = game.PlayerGui.ScreenGui.TextLabel
		pmessage.Text = map.Name .. " has been chosen as the map!"
	end
        message.Text = map.Name .. " has been chosen as the map!"
	    --startgame() calling this will throw a stackoverflow error
	   -- since you are recalling the same function each time you call it
end
startgame()

2 comments

0

Doesn't Work. Sorry

0

Try now.

function startgame()
wait()
if game.Players.NumPlayers >= 1 then
	for _, player in pairs(game.Players:GetPlayers()) do
		local message = player.PlayerGui.ScreenGui.TextLabel
		message.Text = "Getting The Game Ready"
	end
	wait(3)
	local model = game.Lighting.Maps
	local map = model:GetChildren()[math.random(1, #model:GetChildren())]
	message.Text = (map)
	startgame()
	end
end
startgame()

The Second message is not shown? I don't Know why.

This discussion is preserved as it was. New answers, comments, and votes are closed.