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

how to load a profile pic to screen gui?

Asked by 6 years ago

When I click on a TextButton how can I make the localscript load a image of the player who click on the button into a image on the side.

0
I answered for you, hope it helped! superhudhayfa 39 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

I'm going to talk you through this step by step, with the final code at the end.

Okay, let's say you have a ScreenGui in StarterGui and inside of that ScreenGui, you have 3 children. A Local script, a text button and an image label. The first thing you want to do is get the "Players" service, you can do this by using this simple line of code:

local Players = game:GetService("Players")

Now that you have the player service, we need to define the player's username. There are probably easier methods, but the way I did it was like this:

local Players = game:GetService("Players")
local plrName = game.Players.LocalPlayer.Character.Name

Once you have defined the player's username, you need to make a variable for the image itself. To get a player's image, you put this into the image id:

"http://www.roblox.com/Thumbs/Avatar.ashx?x=420&y=420&username=" --followed by their username.

So, using this method and the "plrName" variable we just defined, we can access the player's image.

local Players = game:GetService("Players")
local plrName = game.Players.LocalPlayer.Character.Name
local image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=420&y=420&username=" .. plrName

Now there are only 2 more steps. The function, and the code that goes inside of the function. Fear not, it's as easy as the other code above! The function needs to be launched everytime the TextButton is clicked, so we use the MouseButton1Click method. Alternatively, you can use MouseButton1Down, where you just need to press down the mouse button, but this way they have to press down and let go of the mouse while it's on top of the button. We can do this by:

script.Parent.TextButton.MouseButton1Click:connect(function() end)

So, our code so far is:

local Players = game:GetService("Players")
local plrName = game.Players.LocalPlayer.Character.Name
local image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=420&y=420&username=" .. plrName

script.Parent.TextButton.MouseButton1Click:connect(function() -- Change "script.Parent.TextButton" to the directory of the text button
end)

Now the final bit of code. All we need to do is get the image label's current image and set it to our new image, we do this by first accessing the image label.

script.Parent.ImageLabel

Then getting it's image.

script.Parent.ImageLabel.Image

Then change the image

script.Parent.ImageLabel.Image = image

So our final code would be:

local Players = game:GetService("Players")
local plrName = Players.LocalPlayer.Character.Name
local image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=420&y=420&username=" .. plrName

script.Parent.TextButton.MouseButton1Click:connect(function()
    script.Parent.ImageLabel.Image = image
end)

Or, if you wanted it a bit neater:

local Players = game:GetService("Players")
local plrName = Players.LocalPlayer.Character.Name
local image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=420&y=420&username=" .. plrName
local textButton = script.Parent.TextButton -- Change to directory of the textButton
local imageLabel = script.Parent.ImageLabel -- Change to directory of the image label

function onClicked()
    imageLabel.Image = image
end

textButton.MouseButton1Click:connect(onClicked)

Hope this helped, if there are any issues, please contact me.

Ad

Answer this question