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

How do you make a Gui pop up whenever someone enters the game?

Asked by 9 years ago

Hey! I was trying to make a Player Entered Gui, pop up whenever someone joins the game. This is what I tried:

function findGui(Parent)
    print("Someone entered")
end
game:GetService("Players").PlayerAdded:connect(function(player)
    for _,v in ipairs(game:GetService("Players"):GetChildren()) do
        findGui(v:WaitForChild("PlayerGui").PlayerEntered.Frame)
        chat = Instance.new("TextLabel",v:WaitForChild("PlayerGui").PlayerEntered.Frame)
        chat.Name = "Button"
        chat.Text = player.Name .. " joined the game."
        chat.Size = UDim2.new(0,300,0,40)
        chat.Position = UDim2.new(0,10,0,205)
        chat.Font = "ArialBold"
        chat.FontSize = "Size18"
        chat.BackgroundColor3 = Color3.new(170/255,170/255,127/255)
        chat.BorderColor3 = Color3.new(0/255,0/255,0/255)
        chat.BorderSizePixel = 2
        chat.TextColor3 = Color3.new(0/255,0/255,0/255)
        chat.TextStrokeColor3 = Color3.new(255/255,170/255,0/255)
        chat.TextStrokeTransparency = 0.5
        chat.BackgroundTransparency = 0.3
    end
    findGui(game:GetService("StarterGui").PlayerEntered.Frame)
    chat:Clone().Parent = game:GetService("StarterGui").PlayerEntered.Frame
    wait(3)
    chat:Destroy()
end)

This is inside a regular script, inside Workspace. If anyone could help me solve this problem, I would highly appreciate it! :D

0
Just make a GUI, put it in StarterGui, Done :D fahmisack123 385 — 9y

1 answer

Log in to vote
0
Answered by
noliCAIKS 210 Moderation Voter
9 years ago

There are a couple of issues with this, most of them just being things that could be done better.

Firstly, the problem causing your script not to work, is that the ScreenGui you are relying on is not inside the PlayerGui right as the PlayerGui is added. This can be fixed by changing v:WaitForChild("PlayerGui").PlayerEntered.Frame to v:WaitForChild("PlayerGui"):WaitForChild("PlayerEntered") :WaitForChild("Frame")

Secondly, you seem to be relying on an existing ScreenGui here, while it might be easier to create a new ScreenGui from scratch in the script in this case.

Thirdly, you shouldn't put a copy of the GUI in the StarterGui.

Fourthly, using the findGui function you declared like that is pointless, confusing and counterintuitive. Take it out.

Fifthly, you currently only destroy one of the 'chat' TextLabels after three seconds rather than all of them.

Sixthly, it's probably more efficient to create a prototype for the TextLabel beforehand and just clone it later after changing the text appropriately.

Sevently, your script currently also tries to insert the TextLabel into the player which just joined, and telling them they just joined seems a bit redundant since they are aware of that themself.

Lastly, it's better to use game.Players:GetPlayers() than game.Players:GetChildren() if you want to loop through all the players.

Putting all of this together, this is the solution:

-- Create a prototype for the label beforehand.
playerEnteredLabelProto = Instance.new("TextLabel")
playerEnteredLabelProto.Name = "Button"
playerEnteredLabelProto.Size = UDim2.new(0,300,0,40)
playerEnteredLabelProto.Position = UDim2.new(0,10,0,205)
playerEnteredLabelProto.Font = "ArialBold"
playerEnteredLabelProto.FontSize = "Size18"
playerEnteredLabelProto.BackgroundColor3 = Color3.new(170/255,170/255,127/255)
playerEnteredLabelProto.BorderColor3 = Color3.new(0/255,0/255,0/255)
playerEnteredLabelProto.BorderSizePixel = 2
playerEnteredLabelProto.TextColor3 = Color3.new(0/255,0/255,0/255)
playerEnteredLabelProto.TextStrokeColor3 = Color3.new(255/255,170/255,0/255)
playerEnteredLabelProto.TextStrokeTransparency = 0.5
playerEnteredLabelProto.BackgroundTransparency = 0.3

game:GetService("Players").PlayerAdded:connect(function(newPlayer)
    playerEnteredLabelProto.Text = newPlayer.Name .. " joined the game." -- Set the message of the TextLabel.
    for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
        if player ~= newPlayer then -- Don't show the message to the player that just joined.
            local playerGui = player:FindFirstChild("PlayerGui")
            if playerGui then
                local screenGui = Instance.new("ScreenGui", playerGui) -- Create a new ScreenGui.
                playerEnteredLabelProto:Clone().Parent = screenGui -- Put the TextLabel into the ScreenGui.
                Delay(3, function()
                    screenGui:Destroy() -- Destroy the ScreenGui after 3 seconds.
                end)
            end
        end
    end
end)
Ad

Answer this question