How would I update a on boolean value to the server for a radio system?

Let’s say I need to send to the server everytime a client’s onButton gui says “On”, and vice-versa so when they speak, it comes up on the radio.

If I was to do a remote event, that makes my script for the radio not work so is there an alternative?

1 Like

Sending information from the client to the server is generally done through RemoteEvents and/or RemoteFunctions.

How would these break your radio script, if you don’t mind me asking?

Doing something like this is probably the most efficient way:

(Client)

-- Variables
local replicated_storage = game:GetService("ReplicatedStorage")
local event = replicated_storage.RadioEvent

local button = script.Parent
local on = false

-- Functions
local function Toggle_Radio()
	on = not on
	
	if on then
		event:FireServer(true)
	else
		event:FireServer(false)
	end
end

-- Init
button.MouseButton1Click:Connect(Toggle_Radio)

(Server)

-- Variables
local replicated_storage = game:GetService("ReplicatedStorage")
local event = replicated_storage.RadioEvent

-- Functions
local function Radio_Event(player: Player, value: boolean)
	-- Change server value
end

-- Init
event.OnServerEvent:Connect(Radio_Event)
2 Likes

Well I have two scripts - local & server

In my local script to toggle between the options:

local button = script.Parent

button.MouseButton1Click:Connect(function()
	if button.Text == "Off" then
		-- Nothing
	else
		-- Turn on and then send that to the server
	end
end)

and in my server script (Which I am going to remove the radio command in function of the GUI toggle)

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		local prefix = ":t "
		if message:sub(1, #prefix) == prefix then
			local text = message:sub(#prefix + 1)
			local event = game.ReplicatedStorage.Radio
			event:FireAllClients(player)
			event.PlayerName.Value = ""..game:GetService("ReplicatedStorage").Radio.PlayerName.Value..""
			event.Message.Value = text
		end
	end)
end)

So what boolean value are you tying to update?

oh right i forgot to add it - but basically in the button, theres also a boolean value called “OnSwitch”

So for the local script:

local button = script.Parent

button.MouseButton1Click:Connect(function()
	if button.Text == "Off" then
		button.OnSwitch.Value = true
                 button.Text = "On"
                -- Turn on and then send that to the server
	else
		button.OnSwitch.Value = false
		
	end
end)

Okay, let’s start over.

The code you have provided does this as of now:

Client

  • If the button’s text is “Off”, it changes it to “On” and changes the OnSwitch value to true. The opposite happens when the button is clicked again.

Server

  • Player is added to the game
  • Player chats
  • Checks if the substring of 1 and the number of characters in the prefix string is equal to the prefix string
  • If it is, sets a text variable equal to the substring of the number of characters in the prefix + 1, fires an event to all players passing through the player who chatted, and then sets different values in ReplicatedStorage under the event.

Wow. That was a lot. I am still not sure what you are trying to accomplish here. It seems like there is no correlation between the client and the server at this point.

Yeah I know at the moment - I am trying to configure out a way to make this basically:
Client

  • Toggles button on/off → if it is on, send a request to the server

Server:

  • If the server detects the player’s gui button is On (through the onSwitch boolean value), it can detect the chat of said player, and fire the remote event so it displays on the radio

Basically - removing the subscript part (therefore the use of the :t command), so it’s like how I described.

1 Like

Got it. This makes more sense now.

Try this:

(Client)

-- Variables
local replicated_storage = game:GetService("ReplicatedStorage")
local event = replicated_storage.RadioEvent

local button = script.Parent
local on = false

-- Functions
local function Toggle_Radio()
	on = not on
	
	if on then
		event:FireServer(true)
	else
		event:FireServer(false)
	end
end

-- Init
button.MouseButton1Click:Connect(Toggle_Radio)

(Server)

-- Variables
local replicated_storage = game:GetService("ReplicatedStorage")
local client_event = replicated_storage.RadioEvent
local event = replicated_storage.Radio

local players = game:GetService("Players")

-- Functions
local function Chatted(player: Player, message: string, radio_on: boolean)
	if radio_on then
		event:FireAllClients(player)
		event.PlayerName.Value = ""..game:GetService("ReplicatedStorage").Radio.PlayerName.Value..""
		event.Message.Value = message
	end
end

local function Player_Added(player: Player)
	local radio_on = false
	
	client_event.OnServerEvent:Connect(function(player, value: boolean)
		radio_on = value
	end)
	
	player.Chatted:Connect(function(message)
		Chatted(player, message, radio_on)
	end)
end

-- Init
players.PlayerAdded:Connect(Player_Added)

Using this you can get rid of the OnSwitch value in the button, as that becomes useless.

EDIT: I forgot to add the part where the server checked to see if the radio was on lol.

1 Like

Ah I see now. Thanks for your help!

1 Like