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
01 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
02 | local Players = game:GetService( "Players" ) |
03 |
04 | local remoteEvent = ReplicatedStorage:WaitForChild( "RemoteEventTest" ) |
05 |
06 | local 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 |
09 | end |
10 | Players.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!
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
01 | local part = workspace.Part |
02 |
03 | part.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 |
08 | end ) |
09 |
10 | --[[ |
11 | 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. |
12 | line 6 just fires the remote event to the client |
13 | ]] |
There are probably more methods of firing to the client.
Firing to Server
LocalScript
1 | local remoteEvent = game:GetService( "ReplicatedStorage" ).RemoteEvent |
2 |
3 | local str = "Server" |
4 |
5 | remoteEvent:FireServer(str) |
ServerScript
1 | local remoteEvent = game:GetService( "ReplicatedStorage" ).RemoteEvent |
2 |
3 | remoteEvent.OnServerEvent:Connect( function (playerThatItGotFiredFrom, str) |
4 |
5 | print (str) |
6 | 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
1 | local remoteEvent = game:GetService( "ReplicatedStorage" ).RemoteEvent |
2 |
3 | local str = "test" |
4 | remoteEvent:FireAllClients(str) |
LocalScript
1 | local remoteEvent = game:GetService( "ReplicatedStorage" ).RemoteEvent |
2 |
3 |
4 | remoteEvent.OnClientEvent:Connect( function (str) |
5 | print (str) |
6 | end ) |
This will print 'test' in all client's outputs.
Any questions? Just reply to my answer.
Hi i took your script and altered it.
01 | --serverscript |
02 |
03 | local part = workspace.Part |
04 | local remoteEvent = game.ReplicatedStorage.RemoteEvent |
05 | local gui = game.StarterGui.ScreenGui.Frame |
06 |
07 | part.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 |
13 | end ) |
1 | --localscript |
2 |
3 | local remotevent = game.ReplicatedStorage.RemoteEvent |
4 | local gui = game.StarterGui.ScreenGui.Frame |
5 | local function visiblegui () |
6 | print ( "recieved remote call" ) |
7 | end |
8 |
9 | remotevent.OnClientEvent:Connect(visiblegui) |
doesnt work can you tell why?
it prints 'recieved remote call' but the frame doesnt disappear.