Hi,my remote event is not working for unknown reason,can anyone help me to resolve the question.
The result of print(Player) and print(filter) are the same,it used to work and now it has error in it.
The sender in local script
local button = script.Parent.TextButton
local Player = game.Players.LocalPlayer
local event = game.ReplicatedStorage.Events.NpcEvent.Npc
local filter = 0
button.MouseButton1Click:Connect(function()
event:FireServer(Player,filter)
end)
The receive in serverscript
local event = game.ReplicatedStorage.Events.NpcEvent.Npc
event.OnServerEvent:Connect(function(Player,filter)
print(Player)
print(filter)
end)
(The result of print(Player) and print(filter) are the same (Player) )
Don’t pass the player as an argument from the LocalScript. The server already knows the player that is sending the event, so it is automatically included with the request.
Just as @sleitnick said firing an event or remote already pass the player argument no need to put player argument:
local button = script.Parent.TextButton
local Player = game.Players.LocalPlayer
local event = game.ReplicatedStorage.Events.NpcEvent.Npc
local filter = 0
button.MouseButton1Click:Connect(function()
event:FireServer(filter)
end)
server script:
local event = game.ReplicatedStorage.Events.NpcEvent.Npc
event.OnServerEvent:Connect(function(Player,filter)
print(Player.Name) --put a name here so that the programme wont confuse
print(filter)
end)
When using a RemoteEvent, (client to server), the server automatically gain the Player instance. It means that if you send:
--Client-side
local button = script.Parent.TextButton
local Player = game.Players.LocalPlayer
local event = game.ReplicatedStorage.Events.NpcEvent.Npc
local filter = 0
button.MouseButton1Click:Connect(function()
event:FireServer(Player,filter)
end)
The server will receive by default the Player as 1st parameter, and then will receive all of what you send, in the exact same order.
So the server receives:
function( [Player Instance], [1st element sent], [2nd element sent])
So, if you look, the filter variable instored as parameter in your server side script is equal to Player, and this explain the simily.
So, when doing your FireServer(), you don’t need to pass the Player as an argument since the server will automatically get it as 1st argument.
(and note that it is safer to use the one browsed by the server than the one you send using FireServer().)