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

Can someone help me with my remote event parameters?

Asked by 3 years ago

Hey! I'm messing about with remote events and I am wondering how to make this work.

Client:

local event = game.ReplicatedStorage:WaitForChild("Events").Event

script.Parent.MouseButton1Click:Connect(function()
    event:FireServer(game.Players.LocalPlayer, "Hello")
end)

Server:

local rs = game.ReplicatedStorage
local event = rs:WaitForChild("Events").Event

event.OnServerEvent:Connect(function(player, code)
    if code == "Hello" then
        print("Hi")
    else
        print("Bye")
    end
end)

What I expected it to do is print "Hi". Instead, it printed "Bye."

Please help! Much appreciated.

2 answers

Log in to vote
0
Answered by 3 years ago

When firing from a local script you don't need to specify the player as argument, it is automatically passed by default. The player will still be your first argument when you receive the remote event on your server script, but you don't need it in the client script.

Ad
Log in to vote
0
Answered by
NGC4637 602 Moderation Voter
3 years ago
Edited 3 years ago

in the client, you don't need to do FireServer(game.Players.LocalPlayer, "Hello") , you just do FireServer("Hello")

then at the server, do OnServerEvent(player, code)

so the code was game.Players.LocalPlayer, because you never had to reference localplayer. you just needed to put in the code.

in case you don't know what i mean, here is a fixed version of your code

-- Client

local event = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("Event")

script.Parent.MouseButton1Click:Connect(function()
    event:FireServer("Hello") -- you don't have to add the game.Players.LocalPlayer
end)

-- Server

local rs = game:GetService("ReplicatedStorage")
local event = rs:FindFirstChild("Events").Event

event.OnServerEvent:Connect(function(player, code) -- it already has a player parameter by default. put the player parameter before you add other arguments
    if code == "Hello" then
        print("Hi")
    else
        print("Bye")
    end
end)

Answer this question