Search code examples
luarobloxtextlabel

How can I make changing a text label show to everyone?


My script is:

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
    script.Parent.ClickDetector.MaxActivationDistance = 0
    script.Parent.BrickColor = BrickColor.new("Really red")
    
    local mapfolder = game.Lighting.Folder
    local maps = mapfolder:GetChildren()
    
    chosenmap = maps[math.random(1, #maps)]
    mapclone = chosenmap:Clone()
    local label = plr.PlayerGui.ScreenGui.Frame.TextLabel
    
    if chosenmap.Name == "Blue" then
        label.Text = "A wild Blue Exists!"
    elseif chosenmap.Name == "Yellow" then
        label.Text = "A wild Yellow Exists!"
    elseif chosenmap.Name == "Red" then
        label.Text = "A wild Red Exists!"
    else label.Text = "not ready yet"
    end
    
    mapclone.Parent = workspace
    wait(10)
    mapclone:Destroy()
    script.Parent.BrickColor = BrickColor.new("Lime green")
    script.Parent.ClickDetector.MaxActivationDistance = 32
end)

This works fine however when I test it on a server with people, it doesn't show to them, so how can I make the TextLabel show to everyone?


Solution

  • You're only updating the GUI for your local player. You'll need to iterate through all connected players, then update their GUI as well. https://developer.roblox.com/en-us/api-reference/function/Players/GetPlayers

    Players = game:GetService("Players")
    for i, player in pairs(Players:GetPlayers()) do
        local label = player.PlayerGui.ScreenGui.Frame.TextLabel
        
        if chosenmap.Name == "Blue" then
            label.Text = "A wild Blue Exists!"
        elseif chosenmap.Name == "Yellow" then
            label.Text = "A wild Yellow Exists!"
        elseif chosenmap.Name == "Red" then
            label.Text = "A wild Red Exists!"
        else label.Text = "not ready yet"
        end
    end
    

    game is likely considered a global, so I doubt you'd need to pass it as a function parameter

    script.Parent.ClickDetector.MouseClick:Connect(function(game)
    

    however, I'm certain you won't need plr in there, because you aren't just using your local player.