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

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEventTest")

local function onPlayerAdded(player) -- what does this player thing do??
    -- Fire the remote event
    remoteEvent:FireClient(player) -- whats 'player' its not mentioned in the script
end
Players.PlayerAdded:Connect(onPlayerAdded)
-- 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

local part = workspace.Part

part.Touched:Connect(function(objectThatTouchedMe)
    if objectThatTouchedMe.Parent:FindFirstChild("Humanoid") then
        local client = game.Players:GetPlayerFromCharacter(objectThatTouchedMe.Parent)
        remoteEvent:FireClient(client)
    end
end)

--[[
line 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.
line 6 just fires the remote event to the client
]]

There are probably more methods of firing to the client.

Firing to Server

LocalScript

local remoteEvent = game:GetService("ReplicatedStorage").RemoteEvent

local str = "Server"

remoteEvent:FireServer(str)

ServerScript

local remoteEvent = game:GetService("ReplicatedStorage").RemoteEvent

remoteEvent.OnServerEvent:Connect(function(playerThatItGotFiredFrom, str)

    print(str)
end)

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

local remoteEvent = game:GetService("ReplicatedStorage").RemoteEvent

local str = "test"
remoteEvent:FireAllClients(str)

LocalScript

local remoteEvent = game:GetService("ReplicatedStorage").RemoteEvent


remoteEvent.OnClientEvent:Connect(function(str)
    print(str)
end)

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.

--serverscript

local part = workspace.Part
local remoteEvent = game.ReplicatedStorage.RemoteEvent
local gui = game.StarterGui.ScreenGui.Frame

part.Touched:Connect(function(objectThatTouchedMe)
    if objectThatTouchedMe.Parent:FindFirstChild("Humanoid") then
        local client = game.Players:GetPlayerFromCharacter(objectThatTouchedMe.Parent)
        gui.Visible = false
        remoteEvent:FireClient(client)
    end
end)

--localscript

local remotevent = game.ReplicatedStorage.RemoteEvent
local gui = game.StarterGui.ScreenGui.Frame
local function visiblegui ()
    print("recieved remote call")
end

remotevent.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