I'm trying to get 2 server scripts to communicate with each other. Can I use :FireServer()
with a server-script? Is there a more efficient way to do this?
Ok, what you are trying to do is to make both of the server scripts communicate. But what you are doing, using FireServer from a RemoteEvent to communicate between these scripts is wrong. You should only be using a remote event when communicating between server and client. Try using a Bindable event to make communication between two of the same thing possible.
Here is an example. ServerScript1 is trying to give something to ServerScript2. Using BindableEvent, we can send it as an argument to ServerScript2 for it to print.
ServerScript1:
1 | -- ServerScript 1 |
2 |
3 | local Event = game.Workspace:WaitForChild( "Event" ) |
4 | local Arg 1 = "Solution" |
5 |
6 | wait( 8 ) -- a buffer |
7 |
8 | Event:Fire(Arg 1 ) -- call event with an argument |
ServerScript2:
1 | -- ServerScript 2 |
2 |
3 | local Event = game.Workspace.Event |
4 |
5 | Event.Event:connect( function (Arg 1 ) -- connect function to event |
6 |
7 | print ( "Here is the " ..Arg 1.. " !" ) -- prints out Arg1 |
8 |
9 | end ) |
Pretty simple concept to understand, right?
Hoped this helped solve your question.
--| astrallife |--
Firstly, why do you want a split server script that calls another? Second, you can only use :FireServer() in a localScript, from Local to Server. Just combine both of the ServerScript into one
For example : So you can't do this
1 | local remote = Instance.new( "RemoteEvent" ) |
2 | remote.Name = "HelloThereRemote" |
3 | remote.Parent = game.ReplicatedStorage |
4 | print ( "Hello there" ) |
5 | remote:FireServer() |
.
1 | local remote = game.ReplicatedStorage:WaitForChild( "HelloThereRemote" ) |
2 | remote.OnServerEvent:Connect( function () |
3 | print ( "Hi there" ) |
4 | end ) |
Instead of that, do this
1 | print ( "Hello there" ) |
2 | print ( "Hi there" ) |