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

How do I change the text of a TextLabel in without using a local script?

I am trying to make text appear depending on different scenarios, but I can't get the text to change. I looked this problem up and found that it only works in local scripts, but I cannot make this script local because it is in ServerScriptService. How can I get this to work if it can't be a local script?

Link to explorer image: https://drive.google.com/file/d/1igwetXrLgoLHzFoOCEv3BScwobnpO8O8/view?usp=sharing

Here is the code:

local oldMessage = ""
local minPlayers = 1
local Label = game.StarterGui.ScreenGui.GameMessageLabel
 
function teleportPlayers()
	local TeleportTarget = ""
	local target = CFrame.new(workspace.StartP1.Position)
	print("trying to teleport players")
	for i, player in ipairs(game.Players:GetChildren()) do
		TeleportTarget = (string.format("StartP%s", i))
		target = CFrame.new(workspace[TeleportTarget].Position)
		player.Character.HumanoidRootPart.CFrame = target + Vector3.new(0, i * 5, 0)
		player.Playing.Value = 1
		--add an offset of 5 for each character
	end
end
 
function message(message)
	if oldMessage ~= message then
		oldMessage = message
		print(message)
	end
end
 
function playersCurrentlyPlaying()
	for i, player in ipairs(game.Players:GetChildren()) do
		if player.Playing.Value == 1 then return true end
	end
	return false
end

game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			player.Playing.Value = 0
		end)
	end)
 
	local playing = Instance.new("IntValue", player)
	playing.Value = 0
	playing.Name = "Playing"
end)
while(true) do
	wait(10)
	if #game.Players:getPlayers() >= minPlayers then
		if playersCurrentlyPlaying() then
			Label.Text = "Waiting for an available arena..."
		else
			Label.Text = "There are enough players for a new game!  Teleporting..."
			wait(3)
			Label.Text = "Does this work?"
			wait(4)
			teleportPlayers()
		end
	else 
		Label.Text = "Waiting for more players..." 
	end
end

2 comments

0

Have you tried RemoteEvents? You can use that

0

^

2 answers

✓ Accepted answer

As said in the Comments, One way to do this would be through Remote Events, so you'd need to change this section:

while(true) do
wait(10)
	if #game.Players:GetPlayers() >= minPlayers then
		if playersCurrentlyPlaying() then
			Label.Text = "Waiting for an available arena..."
		else
			Label.Text = "There are enough players for a new game!  Teleporting..."
			wait(3)
			Label.Text = "Does this work?"
			wait(4)
			teleportPlayers()
		end
	else
		Label.Text = "Waiting for more players..."
	end
end

to

Server Script

local remote = game.ReplicatedStorage.RemoteEvent

while true do
wait(10)
	if #game.Players:GetPlayers() >= minPlayers then
		if playersCurrentlyPlaying() then
			local text = "Waiting for an available arena..."
			remote:FireAllClients(text)
		else
			local text = "There are enough players for a new game!  Teleporting..."
			remote:FireAllClients(text)
		wait(3)
			text = "Does this work?"
			remote:FireAllClients(text)
		wait(4)
			teleportPlayers()
		end
	else
		local text = "Waiting for more players..."
		remote:FireAllClients(text)
	end
end

Local Script

local remote = game.ReplicatedStorage.RemoteEvent

remote.OnClientEvent:Connect(function(text)
	script.Parent.Text = text
end)

Where the script.Parent in the LocalScript is the TextLabel

However, as far as your problem in-game, if you are trying to define "Label" as

local Label = game.StarterGui.ScreenGui.GameMessageLabel

You are using the StarterGui, which will not be visible to the player. Guis that the player can see are located in the PlayerGui inside the Player found in game.Players. With this is in mind, if you test a server, you will find that the PlayerGui is on both the server and the client, however, the player will only see a gui on the Client.

Thus, if you used this to get the gui on the server:

local Label = ""

game.Players.PlayerAdded:Connect(function(player)
	local gui = Instance.new("ScreenGui", player.PlayerGui)
	local label = Instance.new("TextLabel", gui)
	label.Name = "GameMessageLabel"
	Label = player.PlayerGui.ScreenGui.GameMessageLabel
end)

The Gui would be manually input, but be invisible to the player.

2 comments

0

I have done what you said but it does not work. I think it might have to do with the word player being underlined, but I don't know how to fix it. Just in case you need it I will put a screenshot of the explorer panel in the question. Thanks

0

Yeah, sry about that I forgot to capitalize the getPlayers to GetPlayers, and to remove the local from one of the texts. Player needs to be defined by an activated function, not within a loop, EDIT: actually you could use fireallclients. I edited it so it should work now

You could have that script change a value ,and then have a local script detect if that value has been changed

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