Issue with variables in radio

Working on a radio script. Working with the variable channel, however it is correctly sending the variable information to the server.

local script:

plr.Chatted:Connect(function(msg)
    if Main.Visible == true then
        channel = "Main"
    elseif LE.Visible == true then
        channel = "LE"
    elseif Training.Visible == true then
        channel = "Training"
    end
    if vis then
                if cs ~= "CallSign" and channel ~= "Unknown" then
                    print(channel)
                    game.ReplicatedStorage.RadioEvents.Send:FireServer(cs, msg, channel)
                end  
    end
end)

The local script is printing the correct channel and printing everything correctly.

server script:

game.ReplicatedStorage.RadioEvents.Send.OnServerEvent:Connect(function(player,cs,msg,channel)
    for i,v in pairs(game:GetService('Players'):GetChildren()) do
        local sendMsg = game.ReplicatedStorage.RadioEvents.Folder.Frame:Clone()
        sendMsg.TextLabel.Text = cs .. " - " .. msg
        print("received transmission: " .. msg .. "channel: " .. channel)
        if channel == "Main" then
            sendMsg.Parent = v.PlayerGui.Radio.Frame.Main    
            print("main")
        elseif channel == "LE" then
            sendMsg.Parent = v.PlayerGui.Radio.Frame.LE
            print("le")
        elseif channel == "Training" then
            sendMsg.Parent = v.PlayerGui.Radio.Frame.Training
            print("training")
        end
        wait(0.3)
        audio:Destroy()
    end
end)

However when print debugging the variable channel at the top, it says attempt to concatenate a local ‘channel’ (a nil value) and does not clone and parent in the next if statement, causing the radio to not function.

Where is the initialization of the channel variable? Because from what I can see if it doesn’t meet any of the conditions then it’s value will be nil.

The first point is in the local script where it finds what the channel is supposed to be.

if Main.Visible == true then
        channel = "Main"
    elseif LE.Visible == true then
        channel = "LE"
    elseif Training.Visible == true then
        channel = "Training"
    end

Thats where it determines the channel

What if it doesn’t meet any of the conditions? I would make it so:

local channel = "Unknown"
if Main.Visible == true then
    channel = "Main"
elseif LE.Visible == true then
    channel = "LE"
elseif Training.Visible == true then
    channel = "Training"
end

One of them is visible at all times. Main, LE, and Training are all frames in a Gui.

Your radio workflow is wrong and it’s also missing text filtering, which is required. Please see the article Text and Chat Filtering. Here are some issues:

  • Using the server to update PlayerGuis: a server’s only involvement with Guis should be replication.

  • Lack of filtering, as mentioned above. Remember that you require text filtering for anything involving user input.

  • No remote security! Your client is authoritative of calls and can tamper with the radio by firing bad or inappropriate messages!

What you should be doing in this case is having the server filter text and then send it back to the client so it can update its own Gui. Ergo:

  • Player chats. Their channel is detected and sent to the server via remote.

  • Server validates player permission to use radio and access channels. If the player can, it moves to it’s actual functions.

  • The server receives the message and performs a text filter on it, retrieving the filtered strings for use. It then calls FireAllClients on itself with the message, or iterates through players and calls FireClient depending on if they pass a conditional check (are they authorised to X and Y or maybe Z?).

  • The client receives the event from the server. It creates a new line in the Radio Gui with the received text getting used.

And thus, your radio.

I already have it filtered.

Here is the updated info:

local script:

plr.Chatted:Connect(function(msg)
	if game.Workspace[plr.Name]:FindFirstChild("Radio") then
		open = true
		mic = true
	else
		open = false
		mic = false
	end
	if Main.Visible == true then
		channel = "Main"
	elseif LE.Visible == true then
		channel = "LE"
	elseif Training.Visible == true then
		channel = "Training"
	end
	if vis then
		if open then
			if mic then
				if cs ~= "CallSign" and channel ~= "Unknown" then
					print(channel)
					game.ReplicatedStorage.RadioEvents.Send:FireServer(cs, msg, channel)
				end  
			end
		end
	end
end)

server script:

game.ReplicatedStorage.RadioEvents.Send.OnServerEvent:Connect(function(player,cs,msg,fromPlayerId,channel)
	for i,v in pairs(game:GetService('Players'):GetChildren()) do
		local TextService = game:GetService("TextService")
		local Text = msg
		local FilteredString = TextService:FilterStringAsync(Text, player.UserId)
		local FilteredText = FilteredString:GetChatForUserAsync(player.UserId)
		
		local sendMsg = game.ReplicatedStorage.RadioEvents.Folder.Frame:Clone()
		sendMsg.TextLabel.Text = cs .. " - " .. FilteredText
		print(channel)
		print("received transmission: " .. msg)
		local audio = game.ReplicatedStorage.RadioEvents.Audio.Send:Clone()
		audio.Parent = v.Character.Head
		audio:Play()
		if channel == "Main" then
			sendMsg.Parent = v.PlayerGui.Radio.Frame.Main	
			print("main")
		elseif channel == "LE" then
			sendMsg.Parent = v.PlayerGui.Radio.Frame.LE
			print("le")
		elseif channel == "Training" then
			sendMsg.Parent = v.PlayerGui.Radio.Frame.Training
			print("training")
		end
		wait(0.3)
		audio:Destroy()
	end
end)

Try incorporating my advice to your code. The server should not be managing Guis for clients.