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

I do not understand how to make a script that fires a remote event from server - client?

Asked by 3 years ago
Edited 3 years ago

Sorry for disturbing everyone again, but i read the developer tutorial on how to use remote events and I am making a game that has a script and a localscript. I want to fire a remote event that runs the localscript (server - client), however after intense researching I still do not know how to make a server - client script. Can anyone explain to me and please provide examples? , i have some questions in the provided script below too! Thanks!

Heres a code on the developer site

01local ReplicatedStorage = game:GetService("ReplicatedStorage")
02local Players = game:GetService("Players")
03 
04local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEventTest")
05 
06local function onPlayerAdded(player) -- what does this player thing do??
07    -- Fire the remote event
08    remoteEvent:FireClient(player) -- whats 'player' its not mentioned in the script
09end
10Players.PlayerAdded:Connect(onPlayerAdded)
11-- what is PlayerAdded?

I also need an in general explanation of server - client and server - all clients and how to use them thanks so much!

0
The PlayerAdded event has a parameter which is the player that just joined the game MarkedTomato 810 — 3y
0
Thanks, so is the playeradded optional? sne_123456 439 — 3y

2 answers

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

Yeah, it's optional this remote event gets fired instantly when the player joins the game.

Firing to client

You can also do this remoteEvent:FireClient(game.Players.MarkedTomato) .

or

Use a TouchedEvent which does

01local part = workspace.Part
02 
03part.Touched:Connect(function(objectThatTouchedMe)
04    if objectThatTouchedMe.Parent:FindFirstChild("Humanoid") then
05        local client = game.Players:GetPlayerFromCharacter(objectThatTouchedMe.Parent)
06        remoteEvent:FireClient(client)
07    end
08end)
09 
10--[[
11line 5 game.Players:GetPlayerFromCharacter which just gets the player from the character I also added a sanity check to see if objectThatTouchedMe.Parent has a humanoid which is the character model.
12line 6 just fires the remote event to the client
13]]

There are probably more methods of firing to the client.

Firing to Server

LocalScript

1local remoteEvent = game:GetService("ReplicatedStorage").RemoteEvent
2 
3local str = "Server"
4 
5remoteEvent:FireServer(str)

ServerScript

1local remoteEvent = game:GetService("ReplicatedStorage").RemoteEvent
2 
3remoteEvent.OnServerEvent:Connect(function(playerThatItGotFiredFrom, str)
4 
5    print(str)
6end)

So in the LocalScript, I just pass the string value into the parentheses. On the server, the first parameter is the player even though I didn't define it Roblox automatically gives me the player that it got fired from so you DON'T need to define the player. I've seen so many people define the player in the LocalScript which you don't need to do. So our first parameter in the LocalScript will be the second in the ServerScript.

Firing to all clients

You can only do this from the Server. This function works exactly like :FireClient() but you don't need to define the player since you're firing to all clients. It works like the Server since all clients experience the same thing all at once.

ServerScript

1local remoteEvent = game:GetService("ReplicatedStorage").RemoteEvent
2 
3local str = "test"
4remoteEvent:FireAllClients(str)

LocalScript

1local remoteEvent = game:GetService("ReplicatedStorage").RemoteEvent
2 
3 
4remoteEvent.OnClientEvent:Connect(function(str)
5    print(str)
6end)

This will print 'test' in all client's outputs.

Any questions? Just reply to my answer.

0
Thanks! sne_123456 439 — 3y
0
Hi please read the below comment! thanks! sne_123456 439 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Hi i took your script and altered it.

01--serverscript
02 
03local part = workspace.Part
04local remoteEvent = game.ReplicatedStorage.RemoteEvent
05local gui = game.StarterGui.ScreenGui.Frame
06 
07part.Touched:Connect(function(objectThatTouchedMe)
08    if objectThatTouchedMe.Parent:FindFirstChild("Humanoid") then
09        local client = game.Players:GetPlayerFromCharacter(objectThatTouchedMe.Parent)
10        gui.Visible = false
11        remoteEvent:FireClient(client)
12    end
13end)
1--localscript
2 
3local remotevent = game.ReplicatedStorage.RemoteEvent
4local gui = game.StarterGui.ScreenGui.Frame
5local function visiblegui ()
6    print("recieved remote call")
7end
8 
9remotevent.OnClientEvent:Connect(visiblegui)

doesnt work can you tell why?

it prints 'recieved remote call' but the frame doesnt disappear.

0
Make it as a question. I can't answer this in the comments MarkedTomato 810 — 3y
0
ok sne_123456 439 — 3y

Answer this question