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?
1 Like
You should manipulate guis under a player’s PlayerGui on the client.
RemoteEvents will work, but you can also just connect the Chatted events on the client.
https://developer.roblox.com/api-reference/class/RemoteEvent
1 Like
I have tried that, but didn’t know you could use player chatted events in a Local Script. I’ll try this, and if it works I’ll mark this as a solution.
1 Like
Works fine for me.
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
1 Like
Would this be for the RemoteEvent script?
1 Like
No, you said you didn’t know you could use player.Chatted events in a local script.
1 Like
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)
1 Like
PlayerAdded events don’t fire for already existing players (including LocalPlayer)
1 Like
Although, it probably would be smarter to use RemoteEvents, because you can only filter text on the server.
1 Like
Would they fire for CharacterAdded?
1 Like
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.
1 Like
I did this to the local script:
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.
1 Like
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.
1 Like
Seriously, stop trying to edit the player’s playergui on the server.
2 Likes
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...
1 Like
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.
Also, you should use text filtering.
https://developer.roblox.com/api-reference/function/Chat/FilterStringAsync
1 Like
Reply to the Text Filtering:
If the person sends a chat, won’t it be filtered first before being used to change the textlabel’s text?
1 Like
.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.
1 Like
Client workload:
- Handle chat event for themselves (not others)
- Pass new chat message along to server
- Update radio Gui with message feed
Server workload:
- 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.
2 Likes
How would I implement this event into either the Local or Remote Controller script?
1 Like