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

How do I make this script work for hats?

script.Parent.MouseButton1Down:connect(function()
playername = script.Parent.Parent.Parent.Parent.Name
player = Workspace:FindFirstChild(""..playername.."")
if player ~= nil then
  player:FindFirstChild("Shirt").ShirtTemplate = "http://www.roblox.com/asset/?id=14729308" 
end
end)

I'm a beginner scripter, so it took me forever just to make this script for shirts, but I can't figure out ow to convert this to hats. Please help.

2 answers

I'm going to ignore how inconvenient your code is because I'll spend hours rambling,

What you could do for example, put in-between your if statements for 'player~=nil'

-- First lets remove the old hats!
for i,v in pairs(player:GetChildren()) do -- Lets get the Player's Characters Children!
	if v:IsA("Hat") then -- Beep beep beep, we found one!
		ypcall(function() -- Why not have some protection?
			v:Destroy() -- Boom, hat is gone!
		end) -- end for ypcall
	end -- end for if statement
end -- end for searching Characters children

-- Now we need the hats you want to give to the player!
local AssetIds = {306968119,150381051} -- Put the hat asset ids into here, I put two example ids in
for i,v in pairs(AssetIds) do -- Going through table
	game:GetService("InsertService"):LoadAsset(v):GetChildren()[1].Parent = player -- Inserting hat and parenting to Players character
end -- End for going through AssetIds table

2 comments

0

didn't work after I tried it

0

That's weird... when I debugged it in studio with 'player=workspace.Player1'' it worked....

I know you didn't come to fix that code you gave us, but, here you go.

script.Parent.MouseButton1Down:connect(function(Player)
Character = Player.Character
if Character ~= nil then
Character:FindFirstChild("Shirt").ShirtTemplate = "http://www.roblox.com/asset/?id=14729308" 
end
end)

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