What is wrong with this code?

I was working on testing a gui that appears and disappears after a few seconds. I made the gui through instance.new, but I can never change the y value of the gui, even with changing it’s Udim2 values and anchor points it still doesn’t work. The script is in ServerScriptService. Am I doing something wrong?
while true do
local ScreenGui = Instance.new(“ScreenGui”, game.StarterGui)
local TextLabel = Instance.new(“TextLabel”, ScreenGui)
TextLabel.Visible = true
TextLabel.Text = “+5 Exp for killing a normal zombie.”
TextLabel.AnchorPoint = Vector2.new(.5, 0)
TextLabel.Position = UDim2.new(.5, 0)
TextLabel.Size = UDim2.new(.5, 0)
TextLabel.BackgroundTransparency = 1
if TextLabel.Visible == false then
if not ScreenGui:Destroy() then

Well, first of all the gui’s parent should be in player.PlayerGui. Second, I think the TextLabel size should be formatted using Udim2 like this: textlabel.Size = UDim2.new(ScaleX, OffsetX, ScaleY, OffsetY)

1 Like

Several things.

  1. You should not be handling GUI stuff on the server; that should be on the client in LocalScripts in local folders. One reason why is because of latency. Communication between the client and the server is slow. The other reason why is because you want the server to do as little work as possible, given that it already has to manage multiple players.

  2. When manually inserting GUIs and displaying them to players, you can’t parent them to StarterGui. StarterGui is only a storage for GUI elements that are cloned to the player’s PlayerGui when the character loads in, where the GUIs are actually displayed.

  3. When instantiating new GUI elements, their size, by default, is UDim2.new(0, 0, 0, 0), in other words, too small to be seen. You must understand what the 4 UDim2 parameters mean and what they represent.

So your solution? You have to parent the GUI elements to game.Players.LocalPlayer and in a LocalScript on the client.

Instantiation of a Udim2 requires 4 parameters and you only passed two. There are 2 parameters for the x-value, and 2 parameters for the y-value. You are only setting the value of x, so you need 2 more parameters if you want to change the y-value.
You should read the wiki page on Udim2s, which can be found here: https://developer.roblox.com/en-us/api-reference/datatype/UDim2

Before you ask us to tell you whether something is wrong, please tell us what the code is even supposed to do or there will be no way to tell you whether it’s wrong !

Why are you using a while loop to handle GUI components?
this will let theoretically infinite ScreenGui s , because you never broke the loop, causing much unnecessary strain on the Server, I suggest you remove the loop , so to let your code function as intended.

Secondly, have you read this: PSA: Don't use Instance.new() with parent argument?

Try to set the instances’ parent separately on a new line, for reasons in the post , through the link , such as the fact that using the Instance.new() with a parent argument queues useless replication which causes inefficient bandwidth usages.

Such as instead of


Try to use this instead :

  local player = game:GetService("Players").LocalPlayer
  local GUI = Instance.new("ScreenGui")

  GUI.Parent = player:WaitForChild("PlayerGui") 

Note that GetService is a good method for certain services, as for example, “Players” are not guaranteed to be instantiated in a certain time, GetService is used to retrieve these Services, as opposed to game.Players, using dot syntax, where if a service isn’t instantiated yet, it would return in an error, “Players is not a valid member” .

Also, as the person above me already mentioned, handling GUI components on the Server is usually not a good idea , try to handle GUI on the client, this would cause fewer latency problems and the Server alone won’t have to handle the GUI for every single player in the Server, resulting in lesser load , and better performance.

Handling GUI through Local Scripts causes the code to be replicated to each Client separately, reducing Server load this way, note that Local Scripts only work in certain places, such as a player’s PlayerGui or StarterPlayerScripts for example.

On another note, a better idea

to handle Gui so that a TextLabel appears through a function, would be to make sure a Text Label containing the desired text already exists, and it’s size and properties are arranged already, just make it’s Visible property = false, through the

Properties Tab


and then manually prompt it by accessing the PlayerGui and the Label and setting it to Visible = true, temporarily.

Here’s a test file from where you can infer more, through a more practical approach, but remember you will have to modify the anonymous function and keep it somewhere in ServerScriptService probably, when trying to incorporate it into your game so it prompts only when a player is killed, right now it will only prompt onMouseButton1Click.

Remember to test it in Play mode.

Hey, one more thing, try to use indentation and format your code nicely so others can help you without having to go through the pain of reading cluttered code !

How do I do that?
  • First type or paste in your code into the text box you type in on the Forum,

  • Next, select it and press this button:


After that, your code should look good. (Preformatted text)

Code-Etiquette :
Code Formatting Etiquette

1 Like