Well, to accomplish this, you can use the LocalPlayer child of a LocalScript, or using multiple script.Parent's, but I don't recommend doing that last one; for both, however, you can use a variable. :)
This is how it would look:
local playerRunningForLocalScript = game.Players.LocalPlayer
As for w/ the script.Parent thingy, it varies depending on each individual script, and it isn't good practice. ;-;
Now that we have our variable for the player, lets begin our quest! :D
W/ there being a variable for the player, how's about we also have a separate variable for the player's character? :O To accomplish this, how's about we get the player's character directly, and use the "CharacterAdded" event as a fail-safe? >:)
local playerRunningForLocalScript = game.Players.LocalPlayer
local playersCharacter = playerRunningForLocalScript.Character or playerRunningForLocalScript.CharacterAdded:wait()
Excellent... >:) Now we're almost done! :D All we need now, is... A variable for the player's torso! :O Gasp
To accomplish getting the player's Torso, we're going to retrieve it from the player's character itself, and we're going to use the WaitForChild function as a fail-safe, in case if it didn't spawn at the time of execution. :)
local playerRunningForLocalScript = game.Players.LocalPlayer
local playersCharacter = playerRunningForLocalScript.Character or playerRunningForLocalScript.CharacterAdded:wait()
local playersTorso = playersCharacter:WaitForChild('Torso')
Alright, now we're ready to begin! :D
...After we do one final touch; create a variable for the GUI! >:O (Play rock music here)
local playerRunningForLocalScript = game.Players.LocalPlayer
local playersCharacter = playerRunningForLocalScript.Character or playerRunningForLocalScript.CharacterAdded:wait()
local playersTorso = playersCharacter:WaitForChild('Torso')
local mainGui = script.Parent
Now, we're officially ready to begin! :D
First off, lets now add our variables:
local playerRunningForLocalScript = game.Players.LocalPlayer
local playersCharacter = playerRunningForLocalScript.Character or playerRunningForLocalScript.CharacterAdded:wait()
local playersTorso = playersCharacter:WaitForChild('Torso')
local mainGui = script.Parent
function click()
???.Torso.BrickColor = BrickColor.new("Really red")
end
script.Parent.MouseButton1Click:connect(click)
Secondly, lets change (now) line 11's script.Parent to mainGui, since it's set to the GUI (AKA its parent :P ):
local playerRunningForLocalScript = game.Players.LocalPlayer
local playersCharacter = playerRunningForLocalScript.Character or playerRunningForLocalScript.CharacterAdded:wait()
local playersTorso = playersCharacter:WaitForChild('Torso')
local mainGui = script.Parent
function click()
???.Torso.BrickColor = BrickColor.new("Really red")
end
mainGui.MouseButton1Click:connect(click)
Thirdly, and just noticed this, lets changed the name of the function, to have it represent its purpose: otherwise, say 5 months from now, would you remember what you were trying to accomplish?! :O
Lets change the name before going on, and have its name, again, represent its purpose. :)
local playerRunningForLocalScript = game.Players.LocalPlayer
local playersCharacter = playerRunningForLocalScript.Character or playerRunningForLocalScript.CharacterAdded:wait()
local playersTorso = playersCharacter:WaitForChild('Torso')
local mainGui = script.Parent
function changePlayersTorsoColorOnClick()
???.Torso.BrickColor = BrickColor.new("Really red")
end
mainGui.MouseButton1Click:connect(changePlayersTorsoColorOnClick)
Much, much better! :D Now, 5 months from now, you may better remember & see what you were trying to accomplish back then! :O
Ok, ok... Enough w/ that. XD
Forthly(?), we're going to change line 8's beginning (???.Torso) to the new variable, which represents the player's torso! :D
local playerRunningForLocalScript = game.Players.LocalPlayer
local playersCharacter = playerRunningForLocalScript.Character or playerRunningForLocalScript.CharacterAdded:wait()
local playersTorso = playersCharacter:WaitForChild('Torso')
local mainGui = script.Parent
function changePlayersTorsoColorOnClick()
playersTorso.BrickColor = BrickColor.new("Really red")
end
mainGui.MouseButton1Click:connect(changePlayersTorsoColorOnClick)
And wa-la! All the icing has been added & cleaned up! :D (Or code..? Something..?) Now, it looks much better, cleaner, easier to read, and now you wont trouble your future self too much. XP
Stuff touched on, but didn't go into great detail about
LocalPlayer -- A LocalScript's Client
LocalScript
Variables
Character (child of a Player)
CharacterAdded Event
WaitForChild Function
Yield - Suspends code (or a thread/ chunk (for a destined amount of time, depending on what type of yield)
Hope this helped you in any way! :D
2 comments
if it's a gui. use script.Parent (add more) to get to the player
Is the script's Parent in a SurfaceGui or a ScreenGui?