I have encountered many instances where I have to completely stop making a game because for the game to properly function the way I intend, I have to use a RemoteEvent and I have no clue how. I have look on the Roblox Developer thing to explain it to me and I understand it can pass stuff between the server and client, but I have no idea how to use it in a script. Could someone possibly dumb it down for me and explain what the things are that I have to use and how to use them in scripts?
It's actually really simple! So I'll explain it to you! This is a very important instance in Roblox if you want to make ScreenGUI's, weapons, tools, building, etc.
So, in my tutorial I will show you how to make a script that spawns a part at the position where the player's mouse is located in the workspace (3D environment).
First we want the player's mouse obviously. So we must have a localscript. This localscript is located in game.StarterPlayer.StarterPlayerScripts.
This is the first step:
local player = game.Players.LocalPlayer local mouse = player:GetMouse()
So that's the first step. Getting the mouse.
Second, we want to create a function that runs once we click the left mousebutton.
local player = game.Players.LocalPlayer local mouse = player:GetMouse() mouse.Button1Down:Connect(function() end)
Great, now we have a function, but it has no function. So let's make that. To get the position of the mouse in the workspace we must use mouse.Hit.
local player = game.Players.LocalPlayer local mouse = player:GetMouse() mouse.Button1Down:Connect(function() local mousepos = mouse.Hit end)
Well, now we want to spawn a Part of course. But we want this to be seen by everyone in the server. So in order to do that, we need a serverscript. But first we need to make a RemoteEvent. We will name this RemoteEvent "SpawnPart" and locate this inside the ReplicatedStorage. Well, how do we send mouse.Hit to the serverscript? Like this!
local player = game.Players.LocalPlayer local mouse = player:GetMouse() mouse.Button1Down:Connect(function() local mousepos = mouse.Hit game.ReplicatedStorage.SpawnPart:FireServer(mousepos) end)
Good, now we need a serverscript. I recommend putting this in serverscriptservice.
First, we want it to be able to receive the information we sent from the localscript. We do it like this:
local Event = game.ReplicatedStorage.SpawnPart Event.OnServerEvent:Connect(function(player, mousepos) end)
Notice how I put "player" on in the parameters on line 3 and didn't type this in the localscript? This is because it's always sent automatically from a localscript.
Great, so now the serverscript receives this information. Now we need it to spawn a part on mousepos.
local Event = game.ReplicatedStorage.SpawnPart Event.OnServerEvent:Connect(function(player, mousepos) local NewPart = Instance.new("Part") NewPart.Parent = game.Workspace NewPart.Size = Vector3.new(3,1,2) NewPart.Position = mousepos.p -- Mouse.Hit is a CFrame, so we need to put .p to get the position end)
That's it. From client to server! Not sure if it works but the RemoteEvent is the subject you're learning here.
From server to client works a little different.
The server uses :FireClient() and :FireAllClients()
I hope you learned from my tutorial. Remember, there are plenty tutorials on YouTube.
I say, just practice.
A RemoteEvent Instance is like a
server COMMAND, for example:
You need to get the LocalPlayer's name since they've been chosen in a random minigame,
you can just do this in a
game.ReplicatedStorage.PlayerPickEvent:FireServer() -- the first argument will always be the player
and then THIS in a
Server Script preferably in
game.ReplicatedStorage.PlayerPickEvent.OnServerEvent:Connect(function(Player) -- first argument is the player print(Player.Name.." was chosen!") end)
This can also be used for example killing a certain player!
game.ReplicatedStorage.PlayerKill.OnServerEvent:Connect(function(Player) Player.Character.Humanoid.Health = 0 end)
Hope this helps!
So, I looked at these, and quite frankly I think people make this over-complicated. So, I'ma simplify this for you. When using
RemoteEvents you should know that it is just like a router. For instance, your
computer is a
client, and the
router is the
server. You can send data to your router, and your router sends data to you.
FireServer For your router to know who sent the data it has to collect the IP, which in this case is the player data so that it can perform the right action with the right address.
FireClient Your router sending stuff to your computer only requires it to send the main data, such as the website's IPs, data trafficking, and others.
FireAllClients Once in a while your router may have to talk to all the computers, so, to do that it just has to find all the IPs
Sometimes, you may need to just move data from one source to another. That is where arguments come in the
data trafficking are arguments. Here is an example:
Computer: Hey router can I access this website
Router: Sure, let me just get it
game.ReplicatedStoarge.AccessWebsite.OnServerEvent:Connect(function(IP,websiteIP) local website = get(websiteIP)
Router: I got the website, sending it to you
-- Part of top piece game.ReplicatedStoarge.SendWebsite:FireClient(IP,website) end)
Computer; Got the website thanks.
game.ReplicatedStoarge.SendWebsite.onClientEvent:Connect(function(website) load(website) end)
Router; Hey computers, I am low on storage
Fire All Clients
Computer: Ok, I am going to alert the user.
game.ReplicatedStoarge.AlertAll.onClientEvent:connect(function(Error) alert(Error) end)