I have this radio system, however when a message is sent 9/10 times it sends 2 messages instead of 1. I’m not sure what’s causing this. Does anyone have any advice on how to fix this or a work around to prevent the same message being sent twice.
I recommend parenting the server script to ServerScriptService instead of ReplicatedStorage. Instances in ReplicatedStorage exist both for the client and the server so it might be causing you the problem. You’ll need to update any variables inside of the script that depend on the parent being ReplicatedStorage to reflect the change to ServerScriptService though else you’ll experience errors
I read your scripts and there doesn’t seem to be anything that could cause the message to be repeated. When you said the message is sent twice 9/10 times, does it work correctly always at the first time you send a message then start to repeat? If yes, than I suspect the OnServerEvent connection is being created inside of another connection which is causing a memory leak and your message to be sent twice after the first time
Are the scripts provided complete or are they snippets of the whole script? If they are snippets, then we’ll most likely need to see the whole script to identify the problem
Edit: @MissBasicallySoft as a test you could also add a print(true) statement inside the OnServerEvent to see if it’s receiving the message twice, and then do the same test in the OnClientEvent and see if true is being printed twice in succession. The statement should be at the top so that the rest of the function doesn’t interfere with the result
There is a bug causing player.Chatted to unexpectedly fire twice. It hasn’t been patched in a while. For now the only way you could fix this is adding a debounce.
I tried to test your suggestion and I found something quite odd because for me player.Chatted only works when used in a server script so I don’t know how it’s working in a LocalScript for @MsBasicallySoft. I’m using TextChatService instead of legacy chat though so that could be the cause
Since player.Chatted works on the server as well you can technically eliminate the clientsend RemoteEvent and do something like this on the server:
local Players = game:GetService("Players")
local allclients = game:GetService("ReplicatedStorage").allclients -- Change this to match the real name
Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(msg)
if radioon == true
and table.find(radiosettings.whitelistedteams, player.Team.Name)
then
local fireablevalue = channel.Value
allclients:FireAllClients(player, msg, fireablevalue)
end
end)
end)