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

Can someone help me understand remote events? They dont work for me

Asked by
TNTIsLyfe 152
4 years ago

I tried to use a remote event for my level up system but i just dont understand them at all :/ this is the local script and server script Local script: (In StarterCharacterScripts)

local Player = game.Players.LocalPlayer
local character = Player.Character
local mouse = Player:GetMouse()
local Humanoid = character.Humanoid
local ammo = 1

local Animation2 = Instance.new("Animation")
Animation2.AnimationId = "rbxassetid://4578997919"
local LoadedAnimation2 = Humanoid:LoadAnimation(Animation2)


mouse.Button1Down:connect(function()
    if ammo == 1 then

    script.Sound:Play()
    LoadedAnimation2:Play()
    game.ReplicatedStorage.ExpGain:FireServer(Player.Name)

ammo = 0
wait (0.3)

ammo = 1
Animation2.Stopped:connect(function()
if ammo == 1 then
Animation2:Play()
end
end)





end 
end)








Server Script (In ServerScriptService)

function PutGui(plr)
local Bill = Instance.new("BillboardGui",)
Bill.Text = "+1"
Bill.ExtensOffset = Vector3.new(0,3,0)
Bill.Parent = plr.Character.Head
wait (0.5)
Bill:Destroy()

end
game.ReplicatedStorage.ExpGain.OnServerEvent:Connect(PutGui)

and Another ServerScript i made (In ServerScriptService

function ExpGain(Plr)
Plr.leaderstats.kills.Value = Plr.leaderstats.kills.Value + 1
end
game.ReplicatedStorage.ExpGain.OnServerEvent:Connect(ExpGain)

i dont really understand remote events so ya :/

0
why do u take so long to post? TNTIsLyfe 152 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

RemoteEvents are to send arguments from the Client Side to the Server Side or Server Side to Client Side. When the server is receiving an argument from the client side, the first argument is the player who fired the event ALWAYS. So, you don't have to put the player as an argument in client side firing the event. When the client is receiving an argument from the server, there are naturally no arguments passed through, so which ever argument you put first in the server event, that will be the first argument in the client event.


In ROBLOX's words, A RemoteEvent is designed to provide a one-way message between the server and clients, allowing Scripts to call code in LocalScripts and vice-versa. This message can be directed from one client to the server, from the server to a particular client, or from the server to all clients.

In order for both the server and clients to utilize a remote event, the RemoteEvent object itself must be in a place where both sides can see it. As such, we recommend that you store the RemoteEvent inside of ReplicatedStorage, although in some cases it’s appropriate to store it in the workspace or inside a Tool.

If you need the result of the call, you should use a RemoteFunction instead. Otherwise a remote event is recommended since it will minimize network traffic/latency and won't yield the script to wait for a response.


I also always put a Folder inside of ReplicatedStorage called Events that I store all RemoteEvents and a Folder called Functions that I store all RemoteFunctions.


For Example: Client -> Server

When a player jumps, it will print the player name and that they have jumped inside of the Server Output Console when you press F9.

LocalScript in StarterGui

--// Variables
local RE = game:GetService("ReplicatedStorage"):WaitForChild("Events")


--// Player Died Event [CLIENT]
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local debounce = true

while wait() do
    if character:WaitForChild("Humanoid").Jump == true and debounce == true then
        debounce = false
        RE.PlayerJumped:FireServer()
        wait(2.5)
        debounce = true
    end
end

ServerScript in ServerScriptService

--// Variables
local RE = game:GetService("ReplicatedStorage"):WaitForChild("Events")


--// Player Died Event [SERVER]
RE.PlayerJumped.OnServerEvent:Connect(function(player)
    print(player.Name.." Has JUMPED!")
end)

RESULT: killerbrenden Has JUMPED!

Now that was Client to Server.


Server to All Clients is a bit different, this time you will have to reference the player as an argument to pass the player into all the client sides.

RemoteEvent:FireAllClients() is where you send all the arguments inside those parentheses to all the clients at the same time.

This is Server -> All Clients

For Example: Server -> All Clients

When a player talks, it will print the player name and the message inside of all the Clients Output console when you press F9.

LocalScript inside of StarterGui

--// Variables
local RE = game:GetService("ReplicatedStorage"):WaitForChild("Events")


--// Player Talked Event [CLIENT]
RE.PlayerTalked.OnClientEvent:Connect(function(player,msg)
    print(player.Name.." Has Said "..msg)
end)

ServerScript inside of ServerScriptService

--// Variables
local RE = game:GetService("ReplicatedStorage"):WaitForChild("Events")


--// Player Talked [SERVER]
game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(msg)
        RE.PlayerTalked:FireAllClients(player,msg)
    end)
end)

RESULT: killerbrenden Has Said Hello.

Now that was Server -> All Clients


Server -> Single Client is mostly the same, but instead of sending all the arguments to all the players, you just send them to a single client.

RemoteEvent:FireClient() is different because you send the arguments to one player from the server, to same player in the client.

This is Server -> Single Client

For Example: Server -> Single Client

When a player joins, a message will show up on their screen and no one elses because we're sending the arguments to that player and no one else. So that one player will receive the arguments and the event will fire the code that's waiting and listening to be fired.

LocalScript inside of StarterGui

--// Variables
local RE = game:GetService("ReplicatedStorage"):WaitForChild("Events")


--// Player Joined Event [CLIENT]
local waittime = 2

RE.PlayerJoined.OnClientEvent:Connect(function(player)
    local localPlayer = game.Players.LocalPlayer
    wait(0.5)
    script.Parent.MessageGui.Enabled = true
    script.Parent.MessageGui.NameDisplay.Text = "Welcome "..localPlayer.Name.."!"
    wait(waittime)
    script.Parent.MessageGui.Enabled = false
    wait(0.25)
    script.Parent.MessageGui:Destroy()
end)

ServerScript inside of ServerScriptService

--// Variables
local RE = game:GetService("ReplicatedStorage"):WaitForChild("Events")


--// Player Joined [SERVER]
game.Players.PlayerAdded:Connect(function(player)
    RE.PlayerJoined:FireClient(player)
end)

I put a ScreenGui inside of StarterGui and named it MessageGui and then I put a textlabel inside of the ScreenGui and named it NameDisplay and I disabled the ScreenGui.

RESULT: https://gyazo.com/204ff24fff7023d56fcafd40955b4aa2


Hope this helped you!

If you have anymore questions, comment down below and I'll help you out! I recommend doing more research using youtube and roblox wiki's! This is only the slim of information I can show you, but I hope this gives you basic knowledge of RemoteEvents and how they work.

If this helped, feel free to mark this as the answer!

~killerbrenden

0
uh can u try make it more simple for me to understand? i still dont get it TNTIsLyfe 152 — 4y
0
I tried your system but it says "Infinite yield possible on ReplicatedStorage:WaitForChild("FolderName")" TNTIsLyfe 152 — 4y
0
I just went into depth on the simple functions of RemoteEvents. Did you change the code and the name of the folder to the same exact words? For Ex: Code WaitForChild("Events") and Folder Name: Events killerbrenden 1537 — 4y
Ad

Answer this question