How would I add text to a GUI multiple times with a for loop?

local RS = game:GetService("ReplicatedStorage")
local Text = script.Parent.Text
RS.LavaEventServer.OnServerEvent:Connect(function()
	for _,plr in pairs(game.Teams.Player:GetPlayers()) do
		
	end
end)

I have this block of code, and inside every time the for loop runs, it gets the information of a player. Then I want it to take that information and add it to the existing text that is part of the GUI.
Example of outcome:
Winners: Jacob, Charlie, Izzabella, Jeremy

local RS = game:GetService("ReplicatedStorage")
local Text = script.Parent.Text

RS.LavaEventServer.OnServerEvent:Connect(function()
	Text = "Winners: "
	
	if #game.Teams.Player:GetPlayers() > 0 then
		for _,plr in pairs(game.Teams.Player:GetPlayers()) do
			if _ < #game.Teams.Player:GetPlayers() then
				Text = str..plr.Name..", "
			else
				Text = str..plr.Name
			end
		end
	else
		Text = "Nobody won!"
	end
end)
local RS = game:GetService("ReplicatedStorage")
local Text = script.Parent.Text
Text = "Winners: "
RS.LavaEventServer.OnServerEvent:Connect(function()
	for _,plr in pairs(game.Teams.Player:GetPlayers()) do
		Text = Text..plr.Name
	end
end)

Something like that will work, if you use "text".. then you can concatenate with other text, sort of like “adding” text onto the end.

The post above has better code though, do if you’re gonna copy either then use his.

What is the str supposed to be?

I have edited my post to use the variable in your original code

There hasn’t been any changes made to the code

what is your script.Parent here is it the text label? or is Text the label?

The script.Parent is the TextLabel, so the variable is refering to the TextLabel’s text

Thinking this will work you need to set the text in the text label after running through players

local RS = game:GetService("ReplicatedStorage")
--local Text = script.Parent.Text  -- if you don't need the current text from this label then you don't need this line

local Text = "Winners: "  -- if no text retrieved from above just set this to start

RS.LavaEventServer.OnServerEvent:Connect(function()
	for i,plr in ipairs(game.Teams.Player:GetPlayers()) do 
		comma = i == 1 and '' or ', '
		Text = Text .. comma .. plr.Name
	end

	script.Parent.Text = Text  -- set the text after concating it
end)

Yes, Player is the name of the team

1 Like

ok that should work just make sure to set the text with that last line back once its all combined

Alright, I’ll look at things and see if this code works

1 Like

Had to edit it didn’t see the , format you had above

ok edited a second should be good now

After tinkering with the code for a while and modifying it to work with the other parts of my game, I have ended up with this:

local RS = game:GetService("ReplicatedStorage")
local TextLabelText = script.Parent.Text 



RS.LavaEventClient.OnClientEvent:Connect(function()
local Text = "Winners: "  
	for i,plr in ipairs(game.Teams.Player:GetPlayers()) do 
		
			print(plr)
			Text = Text..plr.Name..", "
		
	end

	script.Parent.Text = Text  
	game.ReplicatedStorage.WinnerListEvent:FireServer()
	wait(4)
	script.Parent.Text = " "
end)

‘str’ is no longer used, ‘Text’ is used instead.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.