RemoteEvents are used to make communications between a server or client. In simpler terms, it is used for sending signals from a LocalScript to a ServerScript. It has 3 main functions:
This is essential and is used in almost every popular game. Without this, many important functions in any game wouldn't be possible to implement. An example of an important function that (often) uses RemoteEvents are pressing keys to make something happen! (ex: Mouse1 to punch or fire a gun). This is because client-sided events are only visible to one client, and not anyone else.
There are 3 components to fire a RemoteEvent.
The information is sent through parameters. It could be named anything, but it is important to note that in the ServerScript (or receiving script), the first parameter, by default is the player who fired the event!!! THIS IS VERY IMPORTANT LATER ON!
In this tutorial, the LocalScript will be the sender, RemoteEvent will be the medium, and ServerScript will be the receiver. This is known as the FireServer() function.
(For FireClient functions, it is switched around, so that the ServerScript is the sender and the LocalScript is the receiver. This is used to send signals to only one client.)
FireSever() can only be used in LocalScripts. It can be done by doing so:
-- (in a localscript)
game.ReplicatedStorage.RemoteEvent:FireServer(parameter, parameters)
The parameter is the information that will be sent to the Server. It must be received by a ServerScript using the following code:
-- (in a serverscript)
game.ReplicatedStorage.KickPlayer.OnServerEvent:connect(function(parameter, parameters)
print("Hello World!")
end)
It should be noted that the RemoteEvent is usually created and placed into ReplicatedStorage!
Alright, so once we have got a general idea of how RemoteEvents work, let's demonstrate it using an admin panel that can kick players from the game.
First off, make a GUI button that toggles a frame on/off.
script.Parent.MouseButton1Click:Connect(function()
if script.Parent.Parent.Frame.Visible == true then
script.Parent.Parent.Frame.Visible = false
else
script.Parent.Parent.Frame.Visible = true
end
end)
Second, create 2 TextBoxes (NOT TextLabels!) into the frame. One for the player name and one for the reason (for kicking the player). Change the name accordingly in the PlaceHolder property.
Now is the also step to create a RemoteEvent in ReplicatedStorage, A ServerScript in SeverScriptService, and also a TextButton in the frame (The two TextBoxes should be its siblings!) with a localscript as a child. This LocalScript will be the signal sender to the ServerScript in SeverScriptService!.
The product now should look something like this. (Pay attention to the explorer tab too!)
Thirdly, for the communications part:
In the LocalScript whose parent the kick textbutton (will be called senderscript from now on), input this code (An explanation is given at the end of this tutorial!!):
script.Parent.MouseButton1Click:Connect(function()
local playerName = script.Parent.Parent.PLR
local reason = script.Parent.Parent.KICKREASON
local kickedperson = game.Players:FindFirstChild(playerName.Text)
if kickedperson then
game.ReplicatedStorage.KickPlayerEvent:FireServer(playerName.Text, reason.Text)
end
end)
Important: "KickPlayerEvent" must be replaced with whatever the name of your RemoteEvent is! Same applies for every variable and every instance mentioned in the script
Now, for the receiver ServerScipt (will be called receiverscript from now on):
game.ReplicatedStorage.KickPlayerEvent.OnServerEvent:connect(function(player, playerToKick, reason)
print(player, playerToKick, reason)
--not really necessary but it's used for debugging
game.Players:FindFirstChild(playerToKick):Kick(reason)
end)
And we're done! Go ahead and test it out, see how it goes!
Short Extra: You can also make a "kill" function by getting the player's character's humanoid and setting the health to 0 on server event.
Frame Toggle
script.Parent.MouseButton1Click:Connect(function()
if script.Parent.Parent.Frame.Visible == true then
script.Parent.Parent.Frame.Visible = false
else
script.Parent.Parent.Frame.Visible = true
end
end)
Line 1: When the mouse clicks the TextButton, Line 2 - 4: If the Admin GUI is turned on, then turn it off. If it's off, turn it on. Line 5: Ends the "if" condition. Line 6: Ends the code on Line 1.
Kick Signal Sending (From senderscript)
script.Parent.MouseButton1Click:Connect(function()
local playerName = script.Parent.Parent.PLR
local reason = script.Parent.Parent.KICKREASON
local kickedperson = game.Players:FindFirstChild(playerName.Text)
if kickedperson then
game.ReplicatedStorage.KickPlayerEvent:FireServer(playerName.Text, reason.Text)
end
end)
Line 1: When the "kick" button is pressed, Line 2: New variable: The "playerName" TextBox Line 3: New variable: The "reason" TextBox Line 4: Line 5: Find the player in the "Players" Service, and checks if there are any. Line 6: Line 7: If there is a player named accordingly to the "playerName" TextBox, Line 8: Sends the playerName, as well as the reason, to the RemoteEvent. Line 9: Ends the "if" condition. Line 10: Ends the click function.
Kick Signal Receiving (Inside the receiverscript)
game.ReplicatedStorage.KickPlayerEvent.OnServerEvent:Connect(function(player, playerToKick, reason)
print(player, playerToKick, reason)
--not really necessary but it's used for debugging
game.Players:FindFirstChild(playerToKick):Kick(reason)
end)
Line 1: When the RemoteEvent receives a signal from a client, (receiving the playerName and reason) "player" needs to be first in the parameters of the receiving script because by default in Roblox Lua, it will always be the first parameter no matter what you type! Line 2: Used for debugging, it prints the name of the player being kicked as well as the reason! Line 3: The Server kicks the player and provides the written reason!
If you have any questions, feel free to ask below!