Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I am trying to make a GUI button that when clicked it will change the players torso color?

I am making a game and I have a GUI that when a text button is clicked it will change the players torso color.

function click()
	???.Torso.BrickColor = BrickColor.new("Really red")  
end

script.Parent.MouseButton1Click:connect(click)

I have looked on YouTube but I just can't figure out how to get to the player how clicked its model. If I could get some help that would be great. :D

2 comments

0

if it's a gui. use script.Parent (add more) to get to the player

0

Is the script's Parent in a SurfaceGui or a ScreenGui?

3 answers

✓ Accepted answer

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
-- "LocalPlayer" is referenced to a LocalScript as who it's running for (think like in a business: you have a 'client', and you're a worker who's (currently) working for that said client)
-- Variable "playerRunningForLocalScript" is set to the LocalPlayer; btw, you can only get the LocalPlayer from the Players service, and w/ only a LocalScript, b/c attempting to do otherwise will not work & may crash your script as a whole

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()
-- "Character" stands for, and connects to, the player's character, which variable "playersCharacter" is set to
-- Why did I use "playerRunningForLocalScript.CharacterAdded:wait()", you may be wondering? B/c, what if the player's character didn't spawn at time of execution! :O "CharacterAdded" is an event that waits & listens for the player's character to spawn, and the "wait" part is a function that has an event wait & return(?) the information, the player's character in this case; you can also do this w/ other events :)

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')
-- What the WaitForChild function does is that it listens for a child (or object) w/ the same name as the argument given (which is Torso, in this case), and when the child spawns w/in the destined parent (the player's character in this case), it'll return said child, but until then, the WaitForChild function will yield (pause) the code from going any further, which is a great help in the scripting world, and a useful tool! :D
-- Variable "playersTorso" is set to the player's Torso that is w/in the player's Character (mouthful there <->)

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
-- Variable "mainGui" is set to, well, its parent :P

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() -- Ah... Looks much better ;)
	???.Torso.BrickColor = BrickColor.new("Really red")  
end

mainGui.MouseButton1Click:connect(changePlayersTorsoColorOnClick) -- Since the event is set to fire when clicked & fire the function at the same time, we'd better change the name of the old function to the new function ;P

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

  1. LocalPlayer -- A LocalScript's Client

  2. LocalScript

  3. Variables

  4. Character (child of a Player)

  5. CharacterAdded Event

  6. WaitForChild Function

  7. 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

5 comments

0

Wow that was a lot. Thanks so much! :D :D :D great explanation.

0

Also, I couldn't find it. Where did you use Yield in the script?

0

@potatoboy99990 Oh, I didn't use anything called "yield" in the script; the WaitForChild function yields the code until a said child exists w/in said parent. :P Yield just means to suspend; that's what I was talking about.

0

OOOoooh..... xD. err.... I just tried the script a couple times in game with friends an it doesn't work but it works in studio. I am getting noting in output in game... :/

0

@potatoboy99990 Are you using a server-side script, or a localscript? B/c if you're using a server-side and not a localscript, then that's your problem; this script only supports localscripts. :P

If you want the script, here you go.

local player = game.Players.LocalPlayer --variable

local character = player.Character --variable

function click()
character.Torso.BrickColor = BrickColor.new("Really red")
end

script.Parent.MouseButton1Click:connect(click)

~Sincerely, CLVRB

1 comment

1

Do not just post code: explain the reasoning behind your answer, otherwise the OP will not understand what changes you did, and how to accomplish this in the future.

function OnClick()
    game.StarterGUI.Button.Torso.BrickColor=BrickColor.new("Really red")  
end
script.Parent.MouseButton1Click:connect(click)

1 comment

0

If that don't work try a free model.

This discussion is preserved as it was. New answers, comments, and votes are closed.