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

How to script a ScreenGui into the PlayerGui without putting it there manually?

Asked by 5 years ago

Hello! I've been trying to make this simple script work for two days now! Here is my code:

wait(5) potato1 = Instance.new("ScreenGui", game:GetService('Players').spearmint123:WaitForChild('PlayerGui')) print(potato1.Parent) potato2 = Instance.new("TextLabel", potato1) potato2.Name = "HELLO ROBLOX" potato2.Font = "Highway"

I basically aim to insert a ScreenGui object into the PlayerGui (and not the StarterGui, yes, I know the difference). Anyway I can do this without the classic, "ScreenGui is not a valid member of PlayerGui."? I already tried using remote events, remote functions, and dashed in a little bit of localscript to serverscript communication there. Any help is appreciated!!

-Spearmint123

0
wait this is in a local script? Fad99 286 — 5y
0
Oh sorry, this is not in a local script which does not make sense admittedly because then it's server to client and would require remote events/functions, but I have tried almost every deviation so I have tried doing it with a local script and remote events/functions and can provide code if need be. I could be doing anything wrong though, I am new to this. spearmint123 20 — 5y
0
I've done it in a global script. You need to do "FindFirstChildOfClass", but that won't wait for it to be added. So I have created a "WaitForChildOfClass" function if you're curious! Cvieyra2test 176 — 5y

2 answers

Log in to vote
2
Answered by
starmaq 1290 Moderation Voter
5 years ago

Your script is a bit messy, but don't worry i'll show you how to make this clean. First of all you're diffentley gonna need remote events, because as you did in your script. You just named a certain player that you knew his name, what if another player joined, and he's name isn't like yours, it wouldn't work for him, it will work just for the player you refernced, you want it to work for all players, that's why you'll need to use LocalPlayer, and LocalPlayer refers to each client, but it will refer to the client that is currently having the script work; if you wanna simplyfiet, it will pretty much work for any player, also the Instance.new() part is kind of mest up too. So let's starting fixing everything.

Remote Events

Now we got remote events, let me explain these. Remote Events are used to communicate between a client and a server, the client is the player that is currently playing, or pretty much all players, and the server, is roblox, (the server connects between all clients). Now when we need to use these? As I said, when we want a communication between server and client, when we want to exchange information beween them. So for example, in a Server script (which is the normal script) you cant use LocalPlayer, because local player is a local thing, it wouldn't make sense to refer to it from the server. But we can from the client, so we need to transfer it from the client to the server. And that is pretty much the most common thing that you'd want to use remote events for. And that is exactly our case here. Remote Events are always placed in ReplicatedStorage, if you're wondering why, as I said the server has his limitaions, and the client (or local side) has his own limitaions, there are certain places and things that the client can do while the server can't, and vise-versa. If we put our remote event inside PlayerGui, that wouldn't make sense, because that place is local, and it can be only accessed from a local script. That's why we need to put our remote event in a place where both the server and the client can access, and ReplicatedStorage is the place for that, because it is accessed by both. (workspace can work too, but that wouldn't be efficient). And of course when using remote events, you need to have one same remote event connecting the local and server script, that's why sometimes you may have multiple remotes for multiple actions and communications. And remote events aren't only a Server/Client communication, they are also a Server/AllClients and a Client/Client. We also got RemoteFunctions, which aren't that different but have a not so similar way of working. And that's really it in terms of explaining. I didn't explain as much as I wanted because I wanted to keep this as simple as your question. But here is more info, this is a good document on them.

But now let's go to the scripting part. First insert a Remote Event that we will use

This will be your local script

local player = game.Players.LocalPlayer --our local player var
local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent") --this is our remote event
local var = "hi'

--Now here, to send our message to the server, we use the :FireServer function, this will send the information to the server, and we say the server "listen"s to that information, which means he gets it. And the transformation of info from the client to the server is refered to as "Replicate".

remote:FireServer(player, 15212, true, var)
--Now you see, the info that were gonna send is gonna be set as the arguments for the function. and always, always put the player as the first arg, because he is the default arg and not putting him might mest up things. and as you can see its not just players, you can transfer values, instances, variables and many things.

Now this is the server script

local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent") --the same remote that we used

--Now for the server side, we would use an event called .OnServerEvent, and this event, as i said "listen"s or "recieve"s the info recieved from the client side.

Remote.OnServerEvent:Connect(function(plr, num, bool, var) -- and as you can see here we put the args that will represent the info that we sent from the client, and they obbviously dont have to be the same names.
    print(plr)
end)

And that's it, you'll see that it printed the local player succefully.

## The Script

Now this will be your script, don't use the second argument for Instance.new()which represents the parent, it is deprecated (which means outdated) but you can still use it, but it is kind of no efficient. And as we said we're gonna use remoteevents.

--This is your local script
local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent") --also you may wanna rename this cuz youre gonna have a lot of remotes sometimes.
local player = game.Players.LocalPlayer

remote:FireServer(player)


--This is the server side, server script
local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent")

remote.OnServerEvent:Connect(function(player)
    local screengui = Instance.new("ScreenGui")
    screengui.Parent = player:WaitForChild("PlayerGui")
    local textlabel = Instance.new("TextLabel")
    textlabel.Parent = player.PlayerGui.ScreenGui
    textlabel.Name = "Hello World!"
    textlabel.Font = "Highway"
end)

And that's it! You may skip btw, by simply using the .PlayerAdded event, so yeah this is all pointless xd. But it is cooler this way. Anyways this is it.

game.Players.PlayerAdded:Connect(function(player)
    local screengui = Instance.new("ScreenGui")
    screengui.Parent = player:WaitForChild("PlayerGui")
    local textlabel = Instance.new("TextLabel")
    textlabel.Parent = player.PlayerGui.ScreenGui
    textlabel.Name = "Hello World!"
    textlabel.Font = "Highway"
end)

So yeah, that's it! Happy to help, I hope that was a good explanation.

0
Thank you for the thorough response! :) spearmint123 20 — 5y
0
you should accept it as an answer ! :) Fad99 286 — 5y
0
wow thanks a lot people :D you made me happy! starmaq 1290 — 5y
0
and yeah please accept it xd starmaq 1290 — 5y
View all comments (3 more)
0
ty! starmaq 1290 — 5y
0
this answer deserves an upvote ;( starmaq 1290 — 5y
0
people should give more attention to people like you. Well done thanks for helping community AltNature 169 — 4y
Ad
Log in to vote
0
Answered by 5 years ago

you're not parenting the screengui to the player gui

wait(5)  

potato1 = Instance.new(“ScreenGui”) this creates the gui game:GetService("Players").spearmint123:WaitForChild("PlayerGui") then type potato1 .Parent = game.Players.spearmint123.PlayerGui to set its parent print(potato1.Parent)
potato2 = Instance.new(“TextLabel”, potato1)
potato2.Name = “HELLO ROBLOX”
potato2.Font = “Highway”

0
the script shall be a local script so its only for u Gameplayer365247v2 1055 — 5y
0
I tried that and It didn't give me an error, but it didn't work either. And I did parent it to the playerGui, the second param of "Instance.new( _, _)" is which element to parent it to. But I do appreciate the quick response :) spearmint123 20 — 5y
0
this woudlnt work, because it would work for only one player, it wouldnt work for all payers starmaq 1290 — 5y
0
thats what he want since he typed .Spearmint so he obviously only want the gui for himself Gameplayer365247v2 1055 — 5y
0
and thats an innaccurate response... starmaq 1290 — 5y

Answer this question