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
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.
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.
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”