How would I communicate between a script and another script, using remote events?

Hello developers! Recently, I have been trying to make a game modes system, and I want the text to say which game mode was chosen. Only problem is, I don’t know how a script can communicate with another script.

-- script for firing remote event (I know I did it wrong but I don't know how to fix it)
if chosen == "Classic" then
   print("Classic")
   game.ReplicatedStorage.WhichGamemode:FireAllClients(chosen)
   wait(5)
else
   print("Teams")
   game.ReplicatedStorage.WhichGamemode:FireAllClients(chosen)
   wait(5)
end
-- script for showing which gamemode was picked
if i == 5 then 
   game.ReplicatedStorage.WhichGamemode.OnServerEvent:Connect(function(chosen)
   text.Value = (chosen.." gamemode was picked!")
   wait(5)  

Both of these scripts are in serverscriptservice

2 Likes

So basically, what I’m asking is, how would two script communicate with each other?

Not sure if this would help (depending on your full game’s setup), but require()ing a ModuleScript would help you retrieve a value from somewhere else in the game, if not wrong.

1 Like

Bindable Events will be your friends!

You could possibly use Modules, but I prefer and suggest you using Bindables.

4 Likes

You would use a bindable event or a module which you can require and call the function of that module.

You could use Values because it’s can be changed in scripts and can not be changed using client so no exploit can change it.

It could be bad idea but I see it’s good enough

Which function of the event would I use?

If you want scripts to communicate to each other in the same environment like two server scripts then I would look into using BindableEvents:

Script 1

local bindableEvent = PathToBindableEvent

bindableEvent:Fire() -- Fires the BindableEvent

Script 2

local bindableEvent = PathToBindableEvent

bindableEvent.Event:Connect(function(args) -- runs this function when the BindableEvent has been fired
	-- do something
end)

If you want the server to communicate to the client or vice versa then use RemoteEvents or RemoteFunctions for a callback: Custom Events and Callbacks | Documentation - Roblox Creator Hub

13 Likes

Now the problem is that the game restarts after it chooses the gamemode (But the text shows)

When it comes to connecting with scripts and either breaching that client-server (server - client) or breaching that script-script interaction. There are events that you can actually use. These events/functions are called bindable and remote. I’ll be going though what they do and how they’re handy. If you wanted a simpler way to connect scripts. I’d recommend using modules and wrapping them around shared or _G

Basic Information

RBXScriptSignal

An RBXScriptSignal is basically a signal that someone can connect to. This signal when fired will run every function within their connections.

An example of an RBXScriptSignal would be something like: game.Changed, game.ChildAdded, workspace.ChildRemoved

To connect to these ScriptSignals you’d call the method :Connect on the signal. Therefore connecting you to that signal. There is also another feature that these ScriptSignals give to you. They have another method called :Wait. when you call this, the current thread will pause/yield until that event is fired.

RBXScriptSignal

Events

An event is a simple RBXScriptSignal. But instead it has a method called :Fire, This allows you to manually fire this event with custom arguments. Etc.

Handling Events

Functions

This is very much the same as an event. But instead you’d have to call Invoke. The reason behind this is because this Function will return something. You can only assign one function to these. This is because you can’t return more than one object.

Functions

My model example
image

bindables
These are used for communicating script - script. Instead of communicating over the server-client. These instances don’t offer this.

Bindable Event

Now this is basically what I said above. But this is an instance that will talk between two different scripts. I’ll be going though some code examples to clear up some things. I’ll be demonstrating the :Connect feature and the :Wait feature of an Bindable Event. I’ll also go though the :Fire Feature too.

Connect - Managing connections

local Event = script.Parent.Event

Event.Event:Connect(function(TextFromScript_1)
	print(TextFromScript_1)
end)

Yield - Yielding until an event

local Event = script.Parent.Event

Event.Event:Wait()
print("Waited!")

Firing - Firing the event

-- // This is my first script. Which will fire the event.
local Event = script.Parent.Event -- // Defining our event

wait(.5) -- // So script_2 can connect.

Event:Fire("Hello from Script_1") -- // send a cool text over the event.
Bindable Function

Again. This is what I said above. I’ll just go though some code examples and you hopefully should be able to pick up on some things.

Invoke - Invoking the function to return something

-- // This is my first script. Which will fire the event.
local Function = script.Parent.Function -- // Defining our function

wait(.5) -- // So script_2 can connect.

local Sum = Function:Invoke(5, 5) -- // This will return (5 + 5); Calcuated from the script_2
print(Sum) -- // Printing our sum!

OnInvoke - Connecting to the invoke

-- // This is my second script. Which will capture the function.
local Function = script.Parent.Function -- // Defining our function

Function.OnInvoke = function(Number1, Number2) -- // Assigning the function to the invoke.
	return (Number1 + Number2) -- // returning the sum.
end -- // end of function.

remotes
These are used for communicating to client and to server. These are used in basically all games ever since Filtering Enabled was forced.

The whole remote system is quite a lot like the bindable system. So bear in mind. These are basically going to be about the same scripts.

Remote Event

FireClient - Single fireing to a client.

local Event = script.Parent.Event

Event:FireClient(Client, "Etc")

FireAllClients - Firing to all clients in the server

local Event = script.Parent.Event

Event:FireAllClients("Etc")

Connect - Connection to the event
This may get a little big confusing. But i’ll be going though differences on the server and on the client.

-- // Client
local Event = script.Parent.Event

Event.OnClientEvent:Connect(function(Etc)
	
end)

The server gets a bonus argument, This argument is the player that actually fired the remote.

-- // Server
local Event = script.Parent.Event

Event.OnServerEvent:Connect(function(Player, Etc)
	
end)

Yielding - Waiting for the event.

-- // Client
local Event = script.Parent.Event

Event.OnClientEvent:Wait()
-- // Server
local Event = script.Parent.Event

Event.OnServerEvent:Wait()
Remote Function

Invoking

local Function = script.Parent.Function

Function:Invoke(Client, "Etc..")

OnInvoke - Connecting to the invoke

local Function = script.Parent.Function

Function.OnInvoke = function()
	return "Sent from Script_2"
end

my model
Model Example.rbxm (2.6 KB)

By the way. This is just off my general knowledge, Sorry if anyone finds anything wrong/different and feel free to correct me! :smiley:

This also took me like half an hour because i’m very distracted. So sorry if there is any spelling mistakes. :stuck_out_tongue:

1 Like

I’m sorry if I got off track. I lost my focus like 70% though that xD. The way you’d actually talk script-script would be Bindables. These bindables i’ll leave a link too.