i was coding a system that grabs stuff, and i wanna make it serverside, so i made a remote event script that sends the mouse target to the script, but it keeps saying my name instead of the mouse’s target.
heres the script:
local mouse = game.Players.LocalPlayer:GetMouse()
local character = script.Parent
local head = script.Parent:WaitForChild("Head")
local grabevent = script:WaitForChild("GrabEvent")
mouse.Button1Down:Connect(function()
if mouse.Target ~= nil then
if mouse.Target.Parent.Name == "Grabbable" then
if mouse.Target.Anchored == true then
local howfar = math.floor ((head.Position - mouse.Target.Position).Magnitude)
if howfar > 40 then
print("too far")
else
grabevent:FireServer(mouse.Target)
print("yes")
end
end
else
print("nah")
end
end
end)
also sorry some things arent alligned oops
heres the server script:
local grabevent = script.Parent:WaitForChild("GrabEvent")
grabevent.OnServerEvent:Connect(function(mousetarget)
print("hahahha recieved")
print(mousetarget)
end)
1 Like
The first argument passed through a remote event is the player and not your specified arguments
grabevent.OnServerEvent:Connect(function(player, mousetarget) -- The player is the always the first argument passed then its the arguments specified when you fired the event
2 Likes
it still only prints “TehEpicViking” (my username), i added the player variable to both of the scripts
Don’t add it to the script that fires the remote event as I said its always there you don’t have to specifiy it
--local script
grabevent:FireServer(mouse.Target) -- The player will automatically be included as the first argument in this
-- server script
grabevent.OnServerEvent:Connect(function(player, mousetarget) -- If you don't have it like this the script will think "mousetarget" is the player specified and not your argument
1 Like
In the server script, roblox always adds a extra paremeter to remote events.
Instead of “grabevent.OnServerEvent:Connect(function(mousetarget)”
It’s: “grabevent.OnServerEvent:Connect(function(player, mousetarget)”
Roblox adds the parameter. It prints your name because your technically printing your player.
So the working script would be this:
local grabevent = script.Parent:WaitForChild("GrabEvent")
grabevent.OnServerEvent:Connect(function(player, mousetarget)
print("hahahha recieved")
print(mousetarget)
end)
1 Like
yeah this is the correct answer, you always need to put some parameter for the player argument even if you don’t need to use the player, your custom arguments will always be after the player argument.
As the posts above have already explained when using RemoteEvent:FireServer("String")
it moves the arguments given and adds the player that fired the Remote as the first argument.
So instead of sending:
("String")
It sends:
(PlayerThatFiredRemote, "String")
So you will have to add the Player argument to the function connected to the event:
RemoteEvent.OnServerEvent:Connect(function(StringSend)
print(StringSend)
end)
It should look like this:
RemoteEvent.OnServerEvent:Connect(function(Player, StringSend)
print(Player, StringSend)
end)
It should look like this:
LocalScript:
local mouse = game.Players.LocalPlayer:GetMouse()
local character = script.Parent
local head = character:WaitForChild("Head")
local grabevent = script:WaitForChild("GrabEvent")
mouse.Button1Down:Connect(function()
if mouse.Target ~= nil then
if mouse.Target.Parent.Name == "Grabbable" then
if mouse.Target.Anchored == true then
local howfar = math.floor ((head.Position - mouse.Target.Position).Magnitude)
if howfar > 40 then
print("too far")
else
grabevent:FireServer(mouse.Target)
print("yes")
end
end
else
print("nah")
end
end
end)
ServerScript:
local grabevent = script.Parent:WaitForChild("GrabEvent")
grabevent.OnServerEvent:Connect(function(Player, mousetarget)
print("hahahha recieved")
print(mousetarget)
end)
If you still have questions here’s a doc about RemoteEvents!
2 Likes