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

How to use PlayerAdded() in my SystemMessage script?

Asked by 4 years ago

I have code that is supposed to run when a player joins the game and says the player's name in the chat and the same with when a player leaves the game, but I have a dilemma.

The PlayerAdded() event only works on server scripts, but I need it to be a local script because I am trying to send a system message and they only work in local scripts. Is there any way to fix this?

Code:

01game.Players.PlayerAdded:Connect(function(playerAdded)
02 
03    game.StarterGui:SetCore("ChatMakeSystemMessage", {
04        Text = (playerAdded.." has joined the server.");
05        Color = Color3.fromRGB(97, 131, 255);
06        Font = Enum.Font.SciFi
07        })
08end)
09 
10game.Players.PlayerRemoving:Connect(function(playerRemoved)
11 
12    game.StarterGui:SetCore("ChatMakeSystemMessage", { 
13        Text = (playerRemoved.Name.." has left the server.");
14        Color = Color3.fromRGB(97, 131, 255);
15        Font = Enum.Font.SciFi
16    })
17end)
0
You could rely on remote events greatneil80 2647 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago

To achieve this, you have to use a RemoteEvent to work on both the server and the client. The basic layout will be:

Server waits for players to join with PlayerAdded. Server then fires a remote. This remote will be received by the client, who will create the system message. So how do we do it? Read below for a quick example:

Server Script

01local Players = game:GetService("Players")
02local ReplicatedStorage = game:GetService("ReplicatedStorage")
03local Remote = Instance.new("RemoteEvent") --this can also be pre-made by you. this is just an example, though
04Remote.Name = "ChatHandler"
05Remote.Parent = ReplicatedStorage
06 
07Players.PlayerAdded:Connect(function(player)
08    Remote:FireClient(player,"Added") --send to specific player (a required argument) and tell it which message to send
09end)
10 
11Players.PlayerRemoving:Connect(function(player)
12    Remote:FireClient(player,"Removed")
13end)

Local Script

01local StarterGui = game:GetService("StarterGui")
02local Players = game:GetService("Players")
03local Player = Players.LocalPlayer
04local ReplicatedStorage = game:GetService("ReplicatedStorage")
05local Remote = ReplicatedStorage:WaitForChild("ChatHandler") --wait for server to add remote
06 
07Remote.OnClientEvent:Connect(function(action) --"action" will be the parameter for either "Added" or "Removed"
08    if action == "Added" then
09        game.StarterGui:SetCore("ChatMakeSystemMessage", { 
10            Text = (Player.Name.." has joined the server.");
11            Color = Color3.fromRGB(97, 131, 255);
12            Font = Enum.Font.SciFi
13        })
14    elseif action == "Removed" then
15        game.StarterGui:SetCore("ChatMakeSystemMessage", { 
View all 21 lines...

To sum it all up, we have to use a remote here, because we have one situation which needs to be covered on the server-side, and another which needs to be covered locally. Thus, we sent our remote from the server to a specific client (the one joining/leaving). In addition, we sent a specific argument with the remote ("Added" or "Removed"). This eliminates the need for having an entirely separate remote event, just to handle the other end of this code (joining vs. leaving).

If you have any questions, feel free to comment.

Hope this helps!

1
congrats you turned my comment into an answer greatneil80 2647 — 4y
1
congrats u never answered Gey4Jesus69 2705 — 4y
1
Thank you! Green_Lime2 80 — 4y
Ad
Log in to vote
0
Answered by
imKirda 4491 Moderation Voter Community Moderator
4 years ago

PlayerAdded works in Local scripts too and there is no reason why it would not. If you think it did not work because it did not show the message when YOU joined is because PlayerAdded event fires before your script loaded so it did not send message to your client but on all other clients it would show that you have joined. But if you don't need that then use remote events as guy above me said

0
`PlayerAdded` works just fine in local scripts, but it obviously won't fire when the actual local player joins, because the script won't have started yet Gey4Jesus69 2705 — 4y

Answer this question