Script with a Single RemoteEvent doesn't work as intended

I made 2 scripts, one local script and one server script that’s supposed to make a join and leave message for when someone leaves the game.

However, when I tested my scripts, when a player joined it worked properly, but when a player left, it gave the join message instead of the leave message.

What I mean:

{System}: Player1 has joined the game. -- This appears in chat when a player joins
{System}: Player1 has joined the game. -- The same message appears in chat when a player leaves the game as well.

No errors in the output.

The mistake is probably stupidly obvious, but for some reason I can’t identify it.

Can someone tell me what’s wrong with the script(s) and how I can fix them?

Note: I know how to make this work fine with 2 RemoteEvents, but someone told me I only needed one for this, which is why I’m trying to figure out how to make this work.

Here is the Server Script (Located in ServerScriptService):

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local PlayerEvent = ReplicatedStorage:WaitForChild("PlayerEvent")


local function OnPlayerAdded(player)
	 PlayerEvent:FireAllClients(player.Name)
end

local function OnPlayerRemoving(player)
	PlayerEvent:FireAllClients(player.Name)
end

Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoving)

Here is the local script (Located in StarterPlayerScripts):

local Players = game:GetService("Players")
local player =  Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StarterGui = game:GetService("StarterGui")

local PlayerEvent = ReplicatedStorage.PlayerEvent


local function OnPlayerJoined(player)
	StarterGui:SetCore("ChatMakeSystemMessage", {
		Text = "{System}: "..player.." has joined the game.",
		Color = Color3.new(1, 1, 1)
	})
end

local function OnPlayerLeft(player)
	StarterGui:SetCore("ChatMakeSystemMessage", {
		Text = "{System}: "..player.." has left the game.",
		Color = Color3.new(1, 1, 1)
	})
end

PlayerEvent.OnClientEvent:Connect(OnPlayerJoined, OnPlayerLeft)

Your issue is that It cannot fire both, I suggest doing it like this.

– // SERVER

    game.Players.PlayerRemoving:Conncet(function(plr)
       PlayerEvent:FireAllClients(plr.Name,"onPlayerJoined")
    end)
    game.Players.PlayerAdded:Connect(function(plr)
           PlayerEvent:FireAllClients(plr.Name,"onPlayerLeave")
    end)

– // CLIENT


local function onPlayerState(player, state)
        if state ==  "onPlayerJoined" then
              StarterGui:SetCore("ChatMakeSystemMessage", {
		        Text = "{System}: "..player.." has joined the game.",
	         	Color = Color3.new(1, 1, 1)
	     })

            elseif state == "OnPlayerLeave" then
              StarterGui:SetCore("ChatMakeSystemMessage", {
		                 Text = "{System}: "..player.." has left the game.",
		                 Color = Color3.new(1, 1, 1)
	                    })

      end 
end

PlayerEvent.OnClientEvent:Connect(onPlayerState(state)
2 Likes