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