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

Finding a players name?

Asked by 8 years ago

Hey I made a script that will give a player a sword ( clones from lighting to their backpack ) when they click a button (GUI) but I don't know how to find the players name to add the sword into?

so I want to find the players name and clone() the sword into it.
local tool = game.Lighting.Sword
tool.Parent = game.Players.Name.Backpack -- I just put name there..

You see that I put "Name" there it's obviously not going to work. that's the thing I need to find the name of the player who clicked the Button I TRIED EVERYTHING!!!! :)

2 answers

Log in to vote
2
Answered by 8 years ago

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 --The 'LocalPlayer' is the LocalScript's Client; The Variable 'Player' is specifying the Player.
local ServerStorageService = game:GetService("ServerStorage") --It is recommended to use the 'ServerStorage' service to store items such as Tools, Hats, and Models now. :) Variable 'ServerStorageService' is specifying the 'ServerStorage' service
local Tool = ServerStorageService:FindFirstChild("Sword") --Variable 'Tool' is specifying the 'Sword' tool that in which is within the 'ServerStorage' service (confusing explanation ;-; ); The 'FindFirstChild' function (or method) is used in which to check if an Instance is existant within the 'Parent', but, if 'FindFirstChild' did not find a Child with the matching name of the Argument, then it will return 'nil'

Player:WaitForChild("Backpack") --This will yield the code until 'Backpack' is existant within the Player (This may be messy ;-; )

function FunctionToGivePlayerTool() --Here is our new function; This will be used to give the Player the 'Tool'
    if Tool and not plr.Backpack:FindFirstChild("Sword") then --This will check to see if 'Tool' is existant within the 'ServerStorage' service, and if the Player does not have the Tool yet, but, if both Arguments (or the conditions) return true, then it will run the following chunk (lines of code)
        local NewTool = Tool:Clone() --Variable 'NewTool' is specifying the Cloned Tool; The function (or method) 'Clone' is used to, well, 'Clone' specific instances
        NewTool.Parent = Player.Backpack --This will set the Parent Property of the Tool to the 'Player's Backpack
    end --Ends the chunk for the 'if' statement
end --Ends the chunk for the function

script.Parent.TextButton.MouseButton1Down:connect(FunctionToGivePlayerTool) --This will now connect the 'MouseButton1Down' even to the 'TextButton' instance; This will fire every time the Player clicks the GUI

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!

0
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 DiamondCookie2 0 — 8y
Ad
Log in to vote
-2
Answered by 8 years ago

Doesn't work.

0
It works thanks! I had to fix "plr" too "player" too adjust to my script or maybe it was an accident? anyways thanks! -- Btw I am making this for a inventory gui I am working on :D DiamondCookie2 0 — 8y

Answer this question