From the looks of your Question, you are not too sure how to get a Player when he/she Clicks a GUI (as you've stated), however, this can be done by using the following (I'm basing my Question on 'ScreenGui''s, sorry if doesn't help):
1. Getting the LocalPlayer (the Client for the LocalScript) for the user (worst explanation for that ever ;-; ).
2. The MouseButton1Down Event, which fires when a user Clicks on a GUI (Only the TextButton and ImageButton Instances use this event).
3. Using Functions to fire the code, instead of having to rewrite the same code over-and-over again.
Note to self: Remember to do better explanations in the future. :P
Now, with all those in mind, let's set up the code;
local Player = game.Players.LocalPlayer
local ServerStorageService = game:GetService("ServerStorage")
local Tool = ServerStorageService:FindFirstChild("Sword")
Player:WaitForChild("Backpack")
function FunctionToGivePlayerTool()
if Tool and not plr.Backpack:FindFirstChild("Sword") then
local NewTool = Tool:Clone()
NewTool.Parent = Player.Backpack
end
end
script.Parent.TextButton.MouseButton1Down:connect(FunctionToGivePlayerTool)
Might be one of the worst explanations I've ever given. ;-;
Information left out
1. Functions - Functions are used to perform specific (or certain) tasks (for example, how we used one for the code), and can be called by their Function Name (in the Code, the Functions name is FunctionToGivePlayerTool, so if we were to call it by name, it would turn out likeFunctionToGivePlayerTool()), also reduces the use of having to re-write the same code over-and-over again repeatedly, while with a Function you can fire it multiple time (it Fires immediately when Executed (or when called on)) (and can also be fired multiple times on Execution, depending on the Chunk).
2. The If Statement - The If statement is used to check if the Condition is equal to the Argument, or returns true (for the task it performs), and if so, it will run the next Chunk, however, in the case where the Condition is not equal to the Argument, or does not return true, then that's where else (or elseif) comes in; else keyword is used to check (for the 'if' statement) if the Condition returns false, and to run a separate chunk (if given a separate chunk, however, with out the 'else', it will not run any other code).
3. The WaitForChild Method - The WaitForChild method is used to Yield a chunk of code until the Child with the matching name as the Argument is existant within the Parent (as for Example, like in the code, Player:WaitForChild("Backpack"), in this case, it will yield the code until 'Backpack' is existant within the Player), and will return the Child when existant, it will never (or not supposed to) return 'nil'
4. The FindFirstChild Method - The FindFirstChild method is a method which is highly recommended to use if a Child is, or is not, existant within the Parent. The 'FindFirstChild' method will check to see if a Child with the same same as the Argument is existant within the Parent, and if so, will return the Child, however, if there is no such Child, then it will return 'nil' (thus why it is recommended). The FindFirstChild method has two Arguments:
A. The First Argument; The Name to check for the Child that has the same name as the Argument, this is required.
B. The Second Argument; When set to 'true', it will search through the Descendants of the Parent for the Child with the Matching name as the Argument, but if no Child exists then, it will return nil, once again.
That's a lot of information. o_e
I apologize if this doesn't help, or I didn't explain too well. :(
Hope this helped!
1 comment
That was the longest script for this purpose in the history of roblox it's self I haven't used it yet I trust it works! :D