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 3 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:

game.Players.PlayerAdded:Connect(function(playerAdded)

    game.StarterGui:SetCore("ChatMakeSystemMessage", {
        Text = (playerAdded.." has joined the server.");
        Color = Color3.fromRGB(97, 131, 255);
        Font = Enum.Font.SciFi
        })
end)

game.Players.PlayerRemoving:Connect(function(playerRemoved)

    game.StarterGui:SetCore("ChatMakeSystemMessage", {  
        Text = (playerRemoved.Name.." has left the server.");
        Color = Color3.fromRGB(97, 131, 255);
        Font = Enum.Font.SciFi
    })
end)
0
You could rely on remote events greatneil80 2647 — 3y

2 answers

Log in to vote
1
Answered by 3 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

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = Instance.new("RemoteEvent") --this can also be pre-made by you. this is just an example, though
Remote.Name = "ChatHandler"
Remote.Parent = ReplicatedStorage

Players.PlayerAdded:Connect(function(player)
    Remote:FireClient(player,"Added") --send to specific player (a required argument) and tell it which message to send
end)

Players.PlayerRemoving:Connect(function(player)
    Remote:FireClient(player,"Removed")
end)

Local Script

local StarterGui = game:GetService("StarterGui")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = ReplicatedStorage:WaitForChild("ChatHandler") --wait for server to add remote

Remote.OnClientEvent:Connect(function(action) --"action" will be the parameter for either "Added" or "Removed"
    if action == "Added" then
        game.StarterGui:SetCore("ChatMakeSystemMessage", {  
            Text = (Player.Name.." has joined the server.");
            Color = Color3.fromRGB(97, 131, 255);
            Font = Enum.Font.SciFi
        })
    elseif action == "Removed" then
        game.StarterGui:SetCore("ChatMakeSystemMessage", {  
            Text = (Player.Name.." has left the server.");
            Color = Color3.fromRGB(97, 131, 255);
            Font = Enum.Font.SciFi
        })
    end
end)

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 — 3y
1
congrats u never answered Gey4Jesus69 2705 — 3y
1
Thank you! Green_Lime2 80 — 3y
Ad
Log in to vote
0
Answered by
imKirda 4491 Moderation Voter Community Moderator
3 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 — 3y

Answer this question