Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to connect a RemoteEvent?

Asked by 4 years ago

Hello! I am making a game and I wanna know how to connect a RemoteEvent so everyone can see the effect for the attack I am creating.

Script:

01local player = game.Players.LocalPlayer
02local character = script.Parent
03local remoteEvent = game.ReplicatedStorage:WaitForChild("forcebrr")
04local UserInputService = game:GetService("UserInputService")
05local requiredKey = Enum.KeyCode.R
06local rStorage = game.ReplicatedStorage
07 
08UserInputService.InputBegan:Connect(function(key, gameProcessed)
09    if key.KeyCode == requiredKey then
10        --Part Creation, won't waste your time here.
11    end
12end)

I need to know how to bind the event and make it so everyone can see it.

3 answers

Log in to vote
0
Answered by 4 years ago

If you need the part creation, this is it:

01local part = game.ServerStorage.ForceField:Clone()
02        part.Parent = workspace
03        part.Position = character.Torso.Position
04        part.CanCollide = true
05        part.Anchored = true
06        wait(0.01)
07        part.Size = Vector3.new(5.5, 5.5, 5.5)
08        part.Transparency = 0.1
09        wait(0.01)
10        part.Size = Vector3.new(8, 8, 8)
11        part.Transparency = 0.2
12        wait(0.01)
13        part.Size = Vector3.new(10.5, 10.5, 10.5)
14        part.Transparency = 0.3
15        wait(0.01)
View all 38 lines...
Ad
Log in to vote
0
Answered by 4 years ago

Looks like you already have the event so I won't waste your time telling you where to put it.

To begin with, when an event is fired from the server it goes to the client Script -> LocalScript. That being said, when fired from the client it goes to the server LocalScript -> Script.

Judging by line 1 I am guessing that this is a localscript, so what you need to do is fire the event from the client and pick it up on a separate and server sided script. Just remember, localscripts only run on the client and nobody else can see the effects of them, while normal scripts run on the server where everyone can see.

The line of code you'll need for the localscript posted: remoteEvent:FireServer()

-- Explanation: -- When you fire to the server, you can pass certain variables through, but the simple thing I forget to add all the time when teaching people about events, is the fact that the player is ALREADY being passed by default, and does not to be put in manually.

The line of code you'll need for the server script, where you should place all code: game.ReplicatedStorage.forcebrr.OnServerEvent:Connect(function(Player)

-- Explanation: -- (Keep in mind there are a few flaws I will point out now) In the above line I did not include a end), so please apply that. Any code in the server script, when inside that function, will run for every player to see when the event is fired from the client.

If you have any questions or concerns comment, if this helped or worked accept my answer. Have a great day!

Log in to vote
0
Answered by 4 years ago

The code is in a localScript, so it will not be seen by other players except you, so to connect it with a RemoteEvent.

You can create a RemoteEvent within ReplicatedStorage, and a script in ServerScriptService. To activate the RemoteEvent, put the following props in your code.

1local ReplicatedStorage =  game:GetService("ReplicatedStorage")
2local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
3 
4 -- Now inside your function.
5 
6RemoteEvent:FireServer() -- To fire the RemoteEvent.

When we activate a RemoteEvent with the FireServer() method, it automatically sends the player who activated it, then, now in the Script located in ServerScriptService.

1local ReplicatedStorage = game:GetService("ReplicatedStorage")
2local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
3 
4RemoteEvent.OnServerEvent:Connect(function(Player)
5    -- Part Creation Code.
6end)

I hope this has cleared up your doubts. If not, you can ask me :)

0
Thank you very much, I think it worked! I will test it with my friends right now and see if it works. InterDwarf 14 — 4y
0
:) genilsonotavio 132 — 4y
0
If it works, then you can mark it as answered, so you and him get points. R4nd0ml0l2 105 — 4y

Answer this question