#DisplaySystemMessage never runs in LocalScript.

12 messages · Page 1 of 1 (latest)

plush ginkgo
#

This clientside LocalScript receives a remote event from the server when a player joins, the function onPlayerJoin runs yet the DisplaySystemMessage within it never does. There aren't any errors printed so I'm unsure of the issue.

local player = Players.LocalPlayer
local TextChatService = game:GetService("TextChatService")
local ReplicatedFirst = game:GetService("ReplicatedFirst")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remotePlayerJoinEvent = ReplicatedStorage:WaitForChild("RemotePlayerJoinEvent")
local generalChannel: TextChannel = TextChatService:WaitForChild("TextChannels"):WaitForChild("RBXGeneral")
local playerName = player.DisplayName

local function onPlayerJoin()
    if remotePlayerJoinEvent then
        print("[Client] Event received by player", playerName) -- This runs
        generalChannel:DisplaySystemMessage("Hello "..playerName.."!") -- The results of this do not appear in chat.
        remotePlayerJoinEvent:FireServer() -- This too
    else
        warn("[Client] RemotePlayerJoinEvent not found.")
    end
end

remotePlayerJoinEvent.OnClientEvent:Connect(onPlayerJoin)```
vapid trench
# plush ginkgo This clientside LocalScript receives a remote event from the server when a playe...
  1. The server does not need to inform clients of when another client connects to the game. LocalScripts have the capacity to detect the Players.PlayerAdded event.
  2. You're likely testing this code through a solo Roblox Studio play session, which is known to cause some race conditions in this department due to zero latency invited by a locally hosted server; the player is already in the game before the scripts can hook up functions to respond to their joining
#

To solve this:

local function onPlayerAdded(player: Player)
    -- ...
end


for _, player in Players:GetPlayers() do
    task.spawn(onPlayerAdded, player)
end

Players.PlayerAdded:Connect(onPlayerAdded)

Invoke your functions on any players who might already be in the game

plush ginkgo
#

This worked great, I did need to add a task.wait at the start of the for loop, probably because of the race conditions you mentioned. Thank you 🙂

vapid trench
#

Show me your new code

plush ginkgo
#
local player = Players.LocalPlayer
local TextChatService = game:GetService("TextChatService")
local ReplicatedFirst = game:GetService("ReplicatedFirst")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local generalChannel: TextChannel = TextChatService:WaitForChild("TextChannels"):WaitForChild("RBXGeneral")
local playerName = player.DisplayName

local function onPlayerJoin()
    generalChannel:DisplaySystemMessage("[System]: Hello "..playerName.."!")
end

for _, player in Players:GetPlayers() do -- in-case player loads faster than DisplaySystemMessage can run.
    task.wait(1)
    task.spawn(onPlayerJoin, player)
end

Players.PlayerAdded:Connect(onPlayerJoin)```
#

Whenever I run this without the task.wait it does not work.

vapid trench
#

Interesting. After testing this myself, it seems Roblox's new chat system has some race conditions of its own

#

It seems the internal mechanisms take some time to fire up

#

After reviewing the documentation, it does not seem like there is any way for us to know when a text channel is ready to start processing message requests

#

So the last piece of advice I can offer is to simplify the code to:

task.delay(EARLY_BIRD_DELAY, onPlayerAdded, player)