robloxluauroblox-studio

How to edit text of a Text Label on joining a game


I've been trying to help my friend making a randomized title screen splash (like in Minecraft or Terraria) but I've been having some problems with having the text actually change. I've tried a few different methods but none of them have worked so far, these are:

  1. Having everything set up in 1 local script within the main text variable I'm editing with (It prints both the randomized splash number and complete but doesn't change the actual text on the label)
local text = game.StarterGui.MainMenu.splash
local splashText = text.Text
local splash = math.random(1,2)
local event = game.ReplicatedStorage.splash

print(splash)
    
if splash == 1 then
    splashText = "a"
    print("Complete")
elseif splash == 2 then
    splashText = "b"
    print("Complete")
end
  1. Having two different scripts linked with a RemoteEvent, one in "StarterPlayerScripts" and the other in the main element I'm trying to change. All 3 print commands printed to the output log but the text did not change.

Script 1 (Local Script):

local splash = math.random(2,3)
local event = game.ReplicatedStorage.splash

print(splash)
event:FireServer(splash)

Script 2 (Normal Script):

local event = game.ReplicatedStorage.splash

event.OnServerEvent:Connect(function(plr, splash)
    print(splash)
    local text = plr.PlayerGui.MainMenu.splash
    local splashText = text.Text
    if splash == 1 then
        splashText = "a"
                print("Complete")
    elseif splash == 2 then
        splashText = "b"
        print("Complete")
       end
end)
  1. I've replaced the "game.StarterGui.MainMenu.splash" with "game.Players.LocalPlayer.PlayerGui.Mainmenu.splash". It prints all the commands but doesn't change the text.
local text = game.Players.LocalPlayer.PlayerGui.MainMenu.splash
local splashText = text.Text
local splash = math.random(1,2)
local event = game.ReplicatedStorage.splash

print(splash)
    
if splash == 1 then
    splashText = "a"
    print("Complete")
elseif splash == 2 then
    splashText = "b"
    print("Complete")
end
  1. I've tried replacing the variable of "splashText" to be just "text.Text" in both scripts but that also just printed all commands.
local text = game.StarterGui.MainMenu.splash
local splash = math.random(1,2)
local event = game.ReplicatedStorage.splash

print(splash)
    
if splash == 1 then
    text.Text = "a"
    print("Complete")
elseif splash == 2 then
    text.Text = "b"
    print("Complete")
end

Is there any way anybody knows of making this work?


Solution

  • You have to consider that everything in game.StarterGui is cloned to the PlayerGui. Right now you are changing the game.StarterGui.TextLabel but not the one in game.Players.<playername>.PlayerGui.ScreenGui.<...>.

    The file structure is as follows

    StarterGui
    | ScreenGui
    | | Frame
    | | | TextLabel
    | | | | LocalScript
    

    I've used a Frame, but structure as you wish.

    LocalScript

    local textLabel = script.Parent
    local splashTexts = {"foo", "bar"}
    textLabel.Text = splashTexts[math.random(1, #splashTexts)]
    -- uses the length syntax, #, for length of table
    

    Every player will have a different splash text and this will also change every time they respawn (as the PlayerGui is remade every-time they respawn). If this is a problem you can store it as an attribute on the LocalPlayer.

    local textLabel = script.Parent
    local splashTexts = {"foo", "bar"}
    local localPlayer = game:GetService("Players").LocalPlayer
    local chosenText = localPlayer:GetAttribute("SplashText")
    if (chosenText == nil) then
        chosenText = splashTexts[math.random(1, #splashTexts)]
        localPlayer:SetAttribute("SplashText", chosenText)
    end
    textLabel.Text = chosenText