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

How does this script work by finding the exact player that interacted?

Asked by 5 years ago
Edited 5 years ago
script.Parent.OnServerEvent:Connect(function(player)
    local spoon = game.ReplicatedStorage.EraserMaking.Spoon:Clone()
    if spoon.Parent ~= player then
        spoon.Parent = player.Backpack
    end
end)

There is the script, how does it know which player clicked it? It works, I just dont understand how the 'player' function variable is returned to it, and/or how it knows who the player is. It is also obviously connected to a remote event.

Edit: Here is the other half of the remote event if you guys need to see it.

local givercd = game.Workspace.House.Countertop["Spoon Drawer"].ClickDetector

givercd.MouseClick:Connect(function()
    givercd.Parent.RemoteEvent:FireServer()
end)
0
a remote event has 2 halves, wheres the other half? DinozCreates 1070 — 5y
0
bcuz it has player in there so its getting the player that presses it magentacrasyguy -7 — 5y
0
The first argument fed to a RemoteEvent is always the player who fired it. CorruptScript 86 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

In your function, player is a parameter (also called an argument, though there's a difference). When the OnServerEvent is fired, Roblox will send in the arguments (as described in the API) regardless of what name you give those parameters - only order matters. You can change the name of the player parameter to whatever you want (assuming you change the rest of the function to use that new name), so long as you keep it as the first parameter.

[Edit]

ihatecars100 is correct. A Roblox server knows which client it's communicating with (ex they all have a different IP); it has assigned a Player object for each client. So, it only has to look up which Player object is associated with the client it just received a message from and then call your function with that Player object.

0
yes im still confused, how does it know that my character is the parameter if the server calls the function. In my mind the parameter is just nothing since i havent defined it yet videogameland 2 — 5y
0
the client tells the server, "oh can you do this for me" and the server receives this, knowing fully well that the client sent the request. ihatecars100 502 — 5y
0
Ok but how does the server know what to put in the 'player' parameter? Sorry if this is a dumb question I just can't wrap my head around how it does it knows exactly that it's my player that goes in the parameter if I have never told it that it was my player, how does it not confuse me with another player since the parameter is left empty and I have never defined it. videogameland 2 — 5y
0
Just in case, did you see my edit? You don't need to tell Roblox who sent it because Roblox already knows (it keeps track of such information); it can therefore call the function with the proper 'player' argument chess123mate 5873 — 5y
Ad

Answer this question