Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does my GUI fail to display and remove itself correctly?

Asked by
TMGOR 20
7 years ago
local gui= game.StarterGui.plrdiedgui.Frame

game:GetService('Players').PlayerAdded:connect(function(plr)
    plr.CharacterAdded:connect(function(character)
        character:WaitForChild("Humanoid").Died:connect(function()
            wait(0.3)
            local Textlabel = Instance.new("TextLabel")
            Textlabel.Parent = gui
            Textlabel.Position = UDim2.new(0.5, -100,0.5, -125)
            Textlabel.Size = UDim2.new(0, 200,0, 50)
            Textlabel.BackgroundColor3 = BrickColor.White().Color
            Textlabel.BackgroundTransparency = 1
            Textlabel.Text = plr.Name .. tostring(" just died!")
            wait(5)
            Textlabel:remove()

        end)
    end)

This script fails to display this GUI to me until I've respawned, and when the Textlabel is removed, I still see the TextLabel. How do I overcome this?

1 answer

Log in to vote
0
Answered by
jotslo 273 Moderation Voter
7 years ago
Edited 7 years ago

This is one of the most common problems I see in peoples' code.

The Problem? You're attempting to change things in StarterGui. StarterGui is a bin. Everytime a player respawns, everything inside StarterGui is copied over into the player's PlayerGui. You can find out how to change things in PlayerGui here.

If you would like to change something in all players' GUIs, you can run a script like this:

for _, plr in pairs(game.Players:GetChildren()) do
    plr.PlayerGui.ScreenGui.Frame.Visible = true
end

This iterates through all players in Game -> Players and changes the Visible property of a Frame to true in every players' PlayerGui folder.

Feel free to ask any questions in the comment section, I hope this helped!

A few notes:

  • You don't need to convert " just died!" to a string as it already is one, plr.Name .. " just died!" will work absolutely fine.

  • You should never use :remove() in your scripts, it's deprecated and doesn't completely remove objects. Use :Destroy() instead.

0
Thank you, PlayerGUI isn't a server is it? TMGOR 20 — 7y
0
service*** TMGOR 20 — 7y
0
It is not, no jotslo 273 — 7y
0
Would it be fine to create the GUI then parent it to plr.PlayerGUI.ScreenGui.Frame TMGOR 20 — 7y
0
Yes, assuming ScreenGui -> Frame already exists. jotslo 273 — 7y
Ad

Answer this question