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
local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent")
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")
Remote.OnServerEvent:Connect(function(plr, num, bool, var)
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.
local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local player = game.Players.LocalPlayer
remote:FireServer(player)
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.
3 comments
wait this is in a local script?
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.
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!