Hello, my name is HeIIoISteaIUsernames, and I’m new to the Developer Forum. I don’t know my way around the website, so it’d be nice if someone can guide me on certain things.
Anyways, I want to make a “Player has joined the game” message on ROBLOX’s default chat, similar to this.
(but with the message saying: "[Server]: HeIIoISteaIUsernames has joined the game.)
I’ve got my Server speaker all set up and ready to go, but it’s the PlayerAdded event I’m having problems with.
--Services
local ServerScriptService = game:GetService("ServerScriptService")
local Players = game:GetService("Players")
--Modules
local ChatService = require(ServerScriptService:WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))
--Variables
local Server = nil
--Code
if not ChatService:GetChannel("All") then
while true do
local ChannelName = ChatService.ChannelAdded:Wait()
if ChannelName == "All" then
break
end
end
end
--Joining the "All" Channel + Customization
Server = ChatService:AddSpeaker("Server")
Server:JoinChannel("All")
Server:SetExtraData("ChatColor", Color3.fromRGB(255, 255, 255))
Server:SetExtraData("NameColor", Color3.fromRGB(72, 218, 106))
--Player Joined event
Players.PlayerAdded:Connect(function(player)
Server:SayMessage(tostring(player.Name).." has joined the game", "All")
end)
NOTE The output isn’t printing out anything, and the chat isn’t showing anything. I’ve tried numerous attempts to solve the problem solution ,but the result is still the same.
I don’t know how to tackle this problem (yet), so any help is appreciated. Thanks in advanced.
Try adding this at the end so that it goes through every player already in the game that don’t get registered by your PlayerAdded event by the time it starts listening.
for i,v in ipairs(Players:GetChildren()) do
Server:SayMessage(tostring(v.Name).." has joined the game", "All")
end
If you are testing this in Studio, your player is probably joining before the PlayerAdded event is registered. If you are testing this in a live server, then it should be working fine.
I would replace your PlayerAdded event with all of this code:
local function playerAdded(player)
Server:SayMessage(player.Name .. " has joined the game!", "All")
end
game.Players.PlayerAdded:Connect(playerAdded)
for _, player in pairs(game.Players:GetPlayers()) do
playerAdded(player)
end
This runs the PlayerAdded function on all of the current players in the game and all players who subsequently join the game. Sometimes these chat modules don’t run before the first player joins which shows it’s important you account for things like this when dealing with PlayerAdded events upon script initialization.
I’d say that you should be using ipairs when iterating over an array, as per design. pairs is more suggested towards dictionaries.
A drop that canonically, a service should be fetched via GetService and I’d recommend holding it in a variable. You’d do yourself (and collaborators) a solid by assigning your service to a variable, especially when you intend to reuse it later…