So I’m attempting to make a Chat System which sends a system message saying Player has joined the chat using :SendSystemMessage, I’m aware that I need to get the speaker of the player in order to send the System Message. That’s when my problem comes in.
So far what I have just as an example is
game:GetService("Players").PlayerAdded:connect(function(plr)
if chatService ~= nil then
print("Player Has Joined")
for _,v in pairs(game:GetService("Players"):GetPlayers()) do
local PlayerJoinTextColor = Color3.fromRGB(85, 170, 0)
local FriendJoinExtraData = {ChatColor = PlayerJoinTextColor}
chatService:GetSpeaker(tostring(v.Name)):SendSystemMessage("{System} "..plr.Name .. " has joined the game.", "All", FriendJoinExtraData)
end
end
end)
The error with the script above. Is that nothing happens at all. And no, the script is not disabled. I checked by doing
game:GetService("Players").PlayerAdded:connect(function(plr)
for i, v in pairs(game:GetService("Players"):GetPlayers()) do
print("players joined")
end
end)
And it works. Also, I’m aware of the line above printing more than once because it gets all the players in the actual game. I don’t have a fix to this, I wouldn’t mind getting help finding one.
Create a folder in ReplicatedStorage named Events.
Create 2 remote events, one named Added and one named Removing.
3. Add a script in server script service.
And use the code here:
ServerScriptService
local events = game.ReplicatedStorage.Events
game.Players.PlayerAdded:Connect(function(plr)
events.Added:FireAllClients(plr)
end)
game.Players.PlayerRemoving:Connect(function(plr)
events.Removing:FireAllClients(plr)
end)
Create a LocalScript in StarterGui and use this code:
StarterGui, LocalScript
local events = game.ReplicatedStorage.Events
local startergui = game.StarterGui
events.Added.OnClientEvent:Connect(function(plr)
startergui:SetCore("ChatMakeSystemMessage",{
Text = plr.Name.." Has joined the game";
Color = Color3.fromRGB(0,0,0); --Change the color here
Font = Enum.Font.SourceSansBold;
FontSize = Enum.FontSize.Size32;
})
end)
events.Removing.OnClientEvent:Connect(function(plr)
startergui:SetCore("ChatMakeSystemMessage",{
Text = plr.Name.." Has left the game";
Color = Color3.fromRGB(0,0,0); --Change the color here
Font = Enum.Font.SourceSansBold;
FontSize = Enum.FontSize.Size32;
})
end)
Does the print in the first snippet not print either? If so then perhaps chatService variable is nil? Can you show the line where you define it? The ChatService modules are loaded dynamically so they might not be instantly available and you might have to use WaitForChild if requiring them directly and not using the chat modules folder.
System messages in the Lua Chat System do not take extra data. That aside, there’s two SendSystemMessage methods: one for the channel and one for the ChatSpeaker. The former will send one to the whole channel and the latter sends the user a private system message.
In this case, you should be using the channel version of SendSystemMessage as it will cut out most of the work for you. Privately sending system messages if you do not filter player results is reinventing the wheel so it comes down to how you use the chat events.
Now if you’re testing in Studio, players may join ahead of PlayerAdded, so it’s important to run your code against any players that may have already joined the server before your script could have a chance to hook the event.
Here’s some sample code:
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local ChatServiceRunner = ServerScriptService:WaitForChild("ChatServiceRunner")
local ChatService = require(ChatServiceRunner.ChatService)
local function playerAdded(player)
local allChannel = ChatService:GetChannel("All")
allChannel:SendSystemMessage("{System} " .. player.Name .. " joined the game.")
end
Players.PlayerAdded:Connect(playerAdded)
for _, player in ipairs(Players:GetPlayers()) do
playerAdded(player)
end
Now if you wanted extra data for the system speaker, you would have to create your own first using the given API, then send a message from that speaker with the ExtraData table applied.