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

Why won't the script change avatar?

In my code i tried to make it so that when a button is clicked, it changes the local player with whatever id they typed in.However, the code just stops half way through with no roblox error. I am really confused.

local wear = script.Parent.Parent:WaitForChild("WearButton") --button adress
local player = game.Players.LocalPlayer -- local player will be used for both of the following segments --local player

wear.MouseButton1Click:connect(function()
	local Id = script.Parent.Text -- inputted id
	local assetLink = ("https://www.roblox.com/Thumbs/Asset.ashx?	width=110&height=110&assetId="..Id) --asset link, includes ID

	print ("Got to this part at least!")

	local avatarchange = player:GetChildren()-- Getting the children of player


	for i=1, #avatarchange do
		if (avatarchange[i].className == "Pants") then
			print("Got to finding Pants")
			avatarchange[i].PantsTemplate = assetLink -- Not working.... idk why :( , might be the for function itself...

		print("Pants have been succesfully worn")
		end 
	end
end)

print("Passed InsertService for Asset")

Thanks for your help in advance!

1 comment

0

Edited my answer. I know it now!

1 answer

    local assetLink = ("https://www.roblox.com/Thumbs/Asset.ashx?   width=110&height=110&assetId="..Id)

This is asking for a thumbnail of the pants. You cant wear pictures.

Use this instead

    local assetLink = ("rbxassetid://"..Id) 

Or

    local assetLink = ("http://www.roblox.com/asset/?id="..Id) 

Hopefully it should work

Edit:

for i=1, #avatarchange do
        if (avatarchange[i].className == "Pants") then
            print("Got to finding Pants")
            avatarchange[i].PantsTemplate = assetLink
end

Theres the problem. for i=1, #avatarchange do This is asking lua to run the script '#avatarchange' number of times using 'i' as the number of times the script has been run starting from the number 1. However It is not reading the table. for i, v in pairs(avatarchange) do This is the correct script. It tells lua to read the table with 'i' being the index and 'v' being the value. Now let's say local table = {1 = apple} 1 is the index and 'apple' is the value. When roblox assigns 'get children' it assigns the object as the value. so the right script is

for i, v in pairs( avatarchange) do
        if (v.ClassName == "Pants") then
            print("Got to finding Pants")
            v.PantsTemplate = assetLink
end

Do be aware I changed className to ClassName. This might be wrong. I'm not sure

4 comments

0

That seems to have been part of the problem, but apparently not all of it. I put a print command after the for and if statement, and it isn't being reached. Have i set it up incorrectly?

0

Edited :)

0

So there is a difference between a for command that is used for repeating itself x amount of times, and a for loop that has i, v?

0

Thanks for asking. When I think bout it again. There isn't any. Only this way is more formal and easier to handle with table in my opinion. I don't think that part of your script is a problem anymore. I still think that classname might be a prob. try changing it like I said above. with the caps

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