Hey, so I’m currently making a radio GUI. I have a problem with it though. Here’s the script.
local CurrentPlayers = game:GetService('Players'):GetPlayers()
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(msg)
print(msg)
for i, v in next, CurrentPlayers do
if v:FindFirstChild('PlayerGui') then
v.PlayerGui.RadioGUI.Bar.Bcg.l1.Text = ""..player.Name..": "..msg..""
end
end
end)
end)
So what will happen, I will go into the game, send a message, and then it will Print the message, and then nothing will happen to the text box. Any solutions?
local Players = game:GetService"Players"
local function onPlr(plr)
plr.Chatted:Connect(function(msg)
--your OnChatted code
end)
end
Players.PlayerAdded:Connect(onPlr)
local p = Players:GetPlayers()
for i=1,#p do
onPlr(p[i])
end
I used this code and it can’t detect me saying anything.
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(msg)
print(msg)
if script.Parent.OnG.Visible == true then
game.ReplicatedStorage.RadioChat:FireServer(player, msg)
end
end)
end)
CharacterAdded event is an event for a player object, so you must get the player object.
This is why in addition to creating a PlayerAdded event, I looped through all existing players.
local player = game.Players.LocalPlayer
player.Chatted:Connect(function(msg)
print(msg)
if script.Parent.OnG.Visible == true then
game.ReplicatedStorage.RadioChat:FireServer(player.Name, msg)
end
end)
And this to the Remote Event Script:
local RC = game.ReplicatedStorage.RadioChat
local CurrentPlayers = game:GetService('Players'):GetPlayers()
function ChangeTB(player, msg)
for i, v in next, CurrentPlayers do -- Loop through the table
if v:FindFirstChild('PlayerGui') then -- Check if PlayerGui is there
v.PlayerGui.RadioGUI.Bar.Bcg.l1.Text = msg -- Changes the name of Label 1
end
end
end
game.ReplicatedStorage.RadioChat.OnServerEvent:Connect(ChangeTB)
The Local Script can see the message [print(msg)], but won’t replicate to the GUI.
I assume this is in the server, but if you’re getting all the players right when the script runs, then it will get no players (server loads before players are added)
You should get all the players every time.
Uhh, you might want to generate a new list of players every time you call ChangeTB instead of just using an outdated list from the beginning of the game, which will not stay updated with however many players are currently in the game.
Try using this:
local RC = game.ReplicatedStorage.RadioChat
-- local CurrentPlayers = game:GetService("Players"):GetPlayers()
function ChangeTB(player,msg)
local CurrentPlayers = game:GetService("Players"):GetPlayers()
for i, v in next, CurrentPlayers do
--continue on with the rest of your code...
Also, it’s easier to have the server handle the .Chatted events and fire all the clients to update their guis rather than the client fire the server every time the LocalPlayer speaks and have the server attempt to handle the player’s PlayerGuis.
.Chatted events fire with the unfiltered text.
Also, with that RemoteEvent, the client can send whatever they want as a message, even things that aren’t strings.
Validate player should be able to send radio messages
Filter string and pass it to other clients
Filtering only needs to be done on other clients, the sending client is allowed to display an unfiltered version of their message so long as no one else sees it
Don’t edit Guis from the server, this is bad practice and leads to terrible habits. Remotes are necessary for this due to filtering, which is a requirement for any custom input unit.
If you want to handle functions for players, create a function then connect it as necessary.
local Players = game:GetService("Players")
local function onPlayerAdded(Player)
Player.Chatted:Connect(function (Message, Recipient)
-- Your code
end
end
Players.PlayerAdded:Connect(onPlayerAdded) -- Handle new players
for _, Player in pairs(Players:GetPlayers()) do
onPlayerAdded(Player)
end -- Handle existing players
I feel like this thread is getting unnecessarily bloated for what should be a simple problem and solution. At a fundamental level as well as from what the code demonstrates, this equates to creating a custom chat display window. The server’s involvement should only go as far as filtering and passing a string result to other clients. The rest should be handled by the client.
Be sure to read up on the Developer Hub for confusion on any API members. If the articles there don’t answer your questions, feel free to ask about the referenced member or its use in the scenario.