You miss the point of when :FindFirstChildOfClass() is supposed to be used. The method is designed to find a child's ClassName called by the string you had provided in the parenthesis.
There isn't a point to me explaining how to use it since you don't need this to get the player server side (Which is what I think you want to do). Just know that by doing this, this function can return any player since multiple players in Player Service fall under the ClassName 'Player'.
##To solve your question:
Yes, you will need to use a remote event. What's good about a remote event is that it provides you with the player through the OnServerEvent parameters when communicating from client to server.
####You start by setting up your GUI button events and defining remote events in a local script:
local RS = game:GetService("ReplicatedStorage")
local event = RS:WaitForChild("RemoteEvent")
local itemName = "Item"
local function onButtonClicked()
event:FireServer(itemName)
end
script.Parent.MouseButton1Click:Connect(onButtonClicked)
Simple events made with a single :FireServer() command. We also gave a parameter for the item bought. Now for the server side, the server needs to set up the connection whenever a client fires this event.
local RS = game:GetService("ReplicatedStorage")
local event = RS:WaitForChild("RemoteEvent")
local function requestPurchase(player, itemName)
print(player.Name.." has purchased item "..itemName)
end
event.OnServerEvent:Connect(requestPurchase)
Notice that in the OnServerEvent there are two parameters given. First is the player which is what the RemoteEvent (From client to server) ALWAYS provides. The rest are any arguments passed from the client which is just the 'itemName'. You can pass more datatypes through a RemoteEvent like this. There wasn't a need to go to the Player service and get the player from there.
The last thing you wanted was how to get an item to the player's backpack. In my example I had only printed the itemName and player name. This is as simple as using :FindFirstChild() to get a copy of the tool you want inserted to the backpack.
####In the OnServerEvent of the server script:
local desiredTool = game.ServerStorage:FindFirstChild(itemName):Clone()
desiredTool.Parent = player.Backpack
The rest is up to how you set it up. Let me know if I missed anything or you want something explained some more.
2 comments
bro... epic answer
@xPolarium Your entire explanation was beyond helpful. I appreciate the response, honestly.