Any help with very sloppy code not working?

So I have this “code” here and its sloppy and doesnt work, how would I go about making this look nice and work?

	game.Players.PlayerAdded:Connect(function(player)
		player.CharacterAppearanceLoaded:Connect(function(character)
		if player:WaitForChild("leaderstats"):WaitForChild("Hats").Value == 1 then -- Afro
			wait(1)
			local d = character:GetChildren()
			for i=1, #d do
				if (d[i].className == "Accessory") then
					d[i]:remove()
				end
			end
			if character:FindFirstChild("Humanoid") ~= nil then -- Accessory script
				local h = Instance.new("Accessory")
				local p = Instance.new("Part")
				h.Name = "Hat"
				p.Parent = h
				p.Position = character:FindFirstChild("Head").Position
				p.Name = "Handle"
				p.BrickColor = BrickColor.new("Really black") 
				p.formFactor = 0
				p.Size = Vector3.new(-0,-0,-1)
				p.BottomSurface = 0
				p.TopSurface = 0
				p.Locked = true
				script:WaitForChild("AfroMesh"):Clone().Parent = p
				h.Parent = character
				print("It should work")
				h.AttachmentPos = Vector3.new(0, -0.20, 0)
				wait(.1)
	else
if player:WaitForChild("leaderstats"):WaitForChild("Hats").Value == 2 then -- Box
			wait(1)
			local d = character:GetChildren()
			for i=1, #d do
				if (d[i].className == "Accessory") then
					d[i]:remove()
				end
			end
			if character:findFirstChild("Humanoid") ~= nil then -- Accessory script
				local h = Instance.new("Accessory")
				local p = Instance.new("Part")
				h.Name = "Hat"
				p.Parent = h
				p.Position = character:findFirstChild("Head").Position
				p.Name = "Handle"
				p.BrickColor = BrickColor.new("Really black") 
				p.formFactor = 0
				p.Size = Vector3.new(-0,-0,-1) 
				p.BottomSurface = 0
				p.TopSurface = 0
				p.Locked = true
				script.BoxMesh:Clone().Parent = p
				h.Parent = character
				print("It should work")
				h.AttachmentPos = Vector3.new(0, 0.15, -0.01) 
				wait(.1)
else
		if player:WaitForChild("leaderstats"):WaitForChild("Hats").Value == 3 then -- Business
			wait(1)
			local d = character:GetChildren()
			for i=1, #d do
				if (d[i].className == "Accessory") then
					d[i]:remove()
				end
			end
			if character:findFirstChild("Humanoid") ~= nil then -- Accessory script
				local h = Instance.new("Accessory")
				local p = Instance.new("Part")
				h.Name = "Hat"
				p.Parent = h
				p.Position = character:findFirstChild("Head").Position
				p.Name = "Handle"
				p.BrickColor = BrickColor.new("Really black") 
				p.formFactor = 0
				p.Size = Vector3.new(2, 0.8, 2)  
				p.BottomSurface = 0
				p.TopSurface = 0
				p.Locked = true
				script.BusinessMesh:Clone().Parent = p
				h.Parent = character
				print("It should work")
				h.AttachmentPos = Vector3.new(0, 0.15, -0.02) 
				wait(.1)
else
		if player:WaitForChild("leaderstats"):WaitForChild("Hats").Value == 4 then -- Curly
			wait(1)
			local d = character:GetChildren()
			for i=1, #d do
				if (d[i].className == "Accessory") then
					d[i]:remove()
				end
			end
			if character:findFirstChild("Humanoid") ~= nil then -- Accessory script
				local h = Instance.new("Accessory")
				local p = Instance.new("Part")
				h.Name = "Hat"
				p.Parent = h
				p.Position = character:findFirstChild("Head").Position
				p.Name = "Handle"
				p.BrickColor = BrickColor.new("Really black") 
				p.formFactor = 0
				p.Size = Vector3.new(1, 1, 1)   
				p.BottomSurface = 0
				p.TopSurface = 0
				p.Locked = true
				script.CurlyMesh:Clone().Parent = p
				h.Parent = character
				print("It should work")
				h.AttachmentPos = Vector3.new(0.03, 0.1, 0)
				wait(.1)
			else
		if player:WaitForChild("leaderstats"):WaitForChild("Hats").Value == 5 then -- Dread
			wait(1)
			local d = character:GetChildren()
			for i=1, #d do
				if (d[i].className == "Accessory") then
					d[i]:remove()
				end
			end
			if character:findFirstChild("Humanoid") ~= nil then -- Accessory script
				local h = Instance.new("Accessory")
				local p = Instance.new("Part")
				h.Name = "Hat"
				p.Parent = h
				p.Position = character:findFirstChild("Head").Position
				p.Name = "Handle"
				p.BrickColor = BrickColor.new("Really black") 
				p.formFactor = 0
				p.Size = Vector3.new(2,2,2)
				p.BottomSurface = 0
				p.TopSurface = 0
				p.Locked = true
				script.DreadMesh:Clone().Parent = p
				h.Parent = character
				print("It should work")
			    h.AttachmentPos = Vector3.new(0, 0.1, -0)
				wait(.1)
else
		if player:WaitForChild("leaderstats"):WaitForChild("Hats").Value == 6 then -- Emo
			wait(1)
			local d = character:GetChildren()
			for i=1, #d do
				if (d[i].className == "Accessory") then
					d[i]:remove()
				end
			end
			if character:findFirstChild("Humanoid") ~= nil then -- Accessory script
				local h = Instance.new("Accessory")
				local p = Instance.new("Part")
				h.Name = "Hat"
				p.Parent = h
				p.Position = character:findFirstChild("Head").Position
				p.Name = "Handle"
				p.BrickColor = BrickColor.new("Really black") 
				p.formFactor = 0
				p.Size = Vector3.new(2,2,2)
				p.BottomSurface = 0
				p.TopSurface = 0
				p.Locked = true
				script.EmoMesh:Clone().Parent = p
				h.Parent = character
				print("It should work")
				h.AttachmentPos = Vector3.new(0.1, 0.3, -0.1)
				wait(.1)
else
		if player:WaitForChild("leaderstats"):WaitForChild("Hats").Value == 7 then -- Faux"
			wait(1)
			local d = character:GetChildren()
			for i=1, #d do
				if (d[i].className == "Accessory") then
					d[i]:remove()
				end
			end
			if character:findFirstChild("Humanoid") ~= nil then -- Accessory script
				local h = Instance.new("Accessory")
				local p = Instance.new("Part")
				h.Name = "Hat"
				p.Parent = h
				p.Position = character:findFirstChild("Head").Position
				p.Name = "Handle"
				p.BrickColor = BrickColor.new("Really black") 
				p.formFactor = 0
				p.Size = Vector3.new(-0,-0,-1)  
				p.BottomSurface = 0
				p.TopSurface = 0
				p.Locked = true
				script.FauxMesh:Clone().Parent = p
				h.Parent = character
				print("It should work")
				h.AttachmentPos = Vector3.new(0,0.05,0.06)
				wait(.1)
else
		if player:WaitForChild("leaderstats"):WaitForChild("Hats").Value == 8 then -- Longdread
			wait(1)
			local d = character:GetChildren()
			for i=1, #d do
				if (d[i].className == "Accessory") then
					d[i]:remove()
				end
			end
			if character:findFirstChild("Humanoid") ~= nil then -- Accessory script
				local h = Instance.new("Accessory")
				local p = Instance.new("Part")
				h.Name = "Hat"
				p.Parent = h
				p.Position = character:findFirstChild("Head").Position
				p.Name = "Handle"
				p.BrickColor = BrickColor.new("Really black") 
				p.formFactor = 0
				p.Size = Vector3.new(1,1,1)  
				p.BottomSurface = 0
				p.TopSurface = 0
				p.Locked = true
				script.TreckyMesh:Clone().Parent = p
				h.Parent = character
				print("It should work")
				h.AttachmentPos = Vector3.new(0, 0.2, 0) 
				wait(.1)
else
		if player:WaitForChild("leaderstats"):WaitForChild("Hats").Value == 10 then -- Longdread
			wait(1)
			local d = character:GetChildren()
			for i=1, #d do
				if (d[i].className == "Accessory") then
					d[i]:remove()
				end
			end
			if character:findFirstChild("Humanoid") ~= nil then -- Accessory script
				local h = Instance.new("Accessory")
				local p = Instance.new("Part")
				h.Name = "Hat"
				p.Parent = h
				p.Position = character:findFirstChild("Head").Position
				p.Name = "Handle"
				p.BrickColor = BrickColor.new("Really black") 
				p.formFactor = 0
				p.Size = Vector3.new(2,2,2)   
				p.BottomSurface = 0
				p.TopSurface = 0
				p.Locked = true
				script.LongdreadMesh:Clone().Parent = p
				h.Parent = character
				print("It should work")
				h.AttachmentPos = Vector3.new(0, 0.2, 0) 
				wait(.1)
else
		if player:WaitForChild("leaderstats"):WaitForChild("Hats").Value == 9 then -- Longdread
			wait(1)
			local d = character:GetChildren()
			for i=1, #d do
				if (d[i].className == "Accessory") then
					d[i]:remove()
				end
			end
			if character:findFirstChild("Humanoid") ~= nil then -- Accessory script
				local h = Instance.new("Accessory")
				local p = Instance.new("Part")
				h.Name = "Hat"
				p.Parent = h
				p.Position = character:findFirstChild("Head").Position
				p.Name = "Handle"
				p.Material = Enum.Material.SmoothPlastic
				p.formFactor = 0
				p.Size = Vector3.new(-0,-0,-1)
				p.BottomSurface = 0
				p.TopSurface = 0
				p.Locked = true
				script.AfroMesh:Clone().Parent = p
				h.Parent = character
				print("It should work")
				h.AttachmentPos = Vector3.new(0, -0.20, 0)
				wait(.1)
			end
		end
			end
		end
			end
		end
			end
		end
			end
		end
			end
		end
			end
		end
			end
		end
			end
end
			end
		end
		end)
	end)

I really doubt anyone will help, but if you do these things someone will definitely put in the effort:

  1. Describe what it should do and what it is doing instead
  2. There is lots of similar code, move the similar code into functions so it can be reused and make the different parts arguments. You can also pass functions as arguments if the leafs of if statements are different but the middle layers are the same.
  3. Tame the dangerously deep nesting (again, possibly by using functions)
  4. Align if, elseif, else, and ends.
  5. Describe the environment the script is running in
  6. Describe the overall plan to accomplish your goal (how is the script going to do what you want it to)
  7. Optionally add some comments
1 Like

^ This reply is good in terms of making it look nice.

To help us help you fix your script (in terms of functioning) please:

  • Tell us what you’re trying to achieve
  • What the output is (there are some prints in the script, but we don’t know where it breaks)
  • Let us know of any errors outputted to the console

Be careful not to repeat yourself, as it could potentially lead to inefficient code, along with the aesthetic and ease of editing it becoming void with less organisation. :slight_smile:

I am trying to achieve a system of hats that apply to you if you have a specific number in your leaderstat, but I am not sure how to do this, the only output errors is that I cant use elseif.

Can you provide a screenshot of the output?

This sounds fishy but, I am no longer getting anything in the output, just the script not working.

Can you show me the script you’re using to create the leaderstats?

function onPlayerEntered(newPlayer)
	wait(0.1)
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"

	local score1 = Instance.new("IntValue")
	
	score1.Name = "Hats" -- Pooglie Hat
	score1.Value = 0

	score1.Parent = stats
	stats.Parent = newPlayer
	
		local score2 = Instance.new("IntValue")
	
	score2.Name = "Shirt" -- Pooglie Hat
	score2.Value = 0

	score2.Parent = stats
	stats.Parent = newPlayer
	
		local score3 = Instance.new("IntValue")
	
	score3.Name = "Pants" -- Pooglie Hat
	score3.Value = 0

	score3.Parent = stats
	stats.Parent = newPlayer
	
	local score4 = Instance.new("IntValue")
	
	score4.Name = "Face" -- Pooglie Hat
	score4.Value = 0

	score4.Parent = stats
	stats.Parent = newPlayer
	
	local score5 = Instance.new("IntValue")
	
	score5.Name = "HairColor" -- Pooglie Hat
	score5.Value = 0

	score5.Parent = stats
	stats.Parent = newPlayer
		
	local score6 = Instance.new("IntValue")
	
	score6.Name = "SkinColor" -- Pooglie Hat
	score6.Value = 0

	score6.Parent = stats
	stats.Parent = newPlayer
	
	local score7 = Instance.new("IntValue")
	
	score7.Name = "Gender" -- Pooglie Hat
	score7.Value = 0

	score7.Parent = stats
	stats.Parent = newPlayer
	
	local score8 = Instance.new("IntValue")
	
	score8.Name = "Belts" -- Pooglie Hat
	score8.Value = 0

	score8.Parent = stats
	stats.Parent = newPlayer
	
	local score9 = Instance.new("BoolValue")
	
	score9.Name = "DoneTest" -- Pooglie Hat
	score9.Value = false

	score9.Parent = stats
	stats.Parent = newPlayer
	
	local score10 = Instance.new("IntValue")
	
	score10.Name = "Beard" -- Pooglie Hat
	score10.Value = 0

	score10.Parent = stats
	stats.Parent = newPlayer
	
	local score11 = Instance.new("IntValue")
	
	score11.Name = "Glasses" -- Pooglie Hat
	score11.Value = 0

	score11.Parent = stats
	stats.Parent = newPlayer
	
	local score12 = Instance.new("IntValue")
	
	score12.Name = "Watch" -- Pooglie Hat
	score12.Value = 0

	score12.Parent = stats
	stats.Parent = newPlayer
	
	local score13 = Instance.new("IntValue")
	
	score13.Name = "Bag" -- Pooglie Hat
	score13.Value = 0

	score13.Parent = stats
	stats.Parent = newPlayer
	
		local score14 = Instance.new("IntValue")
	
	score14.Name = "Cash" -- Pooglie Hat
	score14.Value = 0

	score14.Parent = stats
	stats.Parent = newPlayer
		
	local score15 = Instance.new("StringValue")
	
	score15.Name = "FirstName" -- Pooglie Hat
	score15.Value = 0

	score15.Parent = stats
	stats.Parent = newPlayer
	
	local score16 = Instance.new("StringValue")
	
	score16.Name = "LastName" -- Pooglie Hat
	score16.Value = 0

	score16.Parent = stats
	stats.Parent = newPlayer
	
	local score17 = Instance.new("StringValue")
	
	score17.Name = "Height" -- Pooglie Hat
	score17.Value = 1

	score17.Parent = stats
	stats.Parent = newPlayer
	
	local score18 = Instance.new("StringValue")
	
	score18.Name = "Weight" -- Pooglie Hat
	score18.Value = 0

	score18.Parent = stats
	stats.Parent = newPlayer
	
	local score19 = Instance.new("BoolValue")
	
	score19.Name = "OwnSedan" -- Pooglie Hat
	score19.Value = false

	score19.Parent = stats
	stats.Parent = newPlayer
	
	local score20 = Instance.new("BoolValue")
	
	score20.Name = "OwnTaxi" -- Pooglie Hat
	score20.Value = false

	score20.Parent = stats
	stats.Parent = newPlayer
	
	local score21 = Instance.new("BoolValue")
	
	score21.Name = "OwnConvertable" -- Pooglie Hat
	score21.Value = false

	score21.Parent = stats
	stats.Parent = newPlayer
end

game.Players.ChildAdded:connect(onPlayerEntered)

Alright. Can you explain what exactly doesn’t work?

I’ve managed to get it to work so that it prints the “it should work”, meaning everything before that has been executed. However, the hat isn’t equipping properly. Is this the issue you’ve been facing or is it something different?

No, I just do not get the hat and no output, but it works when I put like 9 “playeradded” functions which is just a goofy idea, so im trying this new approach.

Weird. I changed the default value to 1 (just to test) and I got output.

The script I used is below (with some indentations now :slight_smile: )

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAppearanceLoaded:Connect(function(character)
		if player:WaitForChild("leaderstats"):WaitForChild("Hats").Value == 1 then -- Afro
			print("value == 1")
			wait(1)
			local d = character:GetChildren()
			for i = 1, #d do
				if (d[i].className == "Accessory") then
					d[i]:remove()
				end
			end
			
			if character:FindFirstChild("Humanoid") ~= nil then -- Accessory script
				print("not nil")
				local h = Instance.new("Accessory")
				local p = Instance.new("Part")
				h.Name = "Hat"
				p.Parent = h
				p.Position = character:FindFirstChild("Head").Position
				p.Name = "Handle"
				p.BrickColor = BrickColor.new("Really black") 
				p.formFactor = 0
				p.Size = Vector3.new(-0,-0,-1)
				p.BottomSurface = 0
				p.TopSurface = 0
				p.Locked = true
				script:WaitForChild("AfroMesh"):Clone().Parent = p
				h.Parent = character
				print("It should work")
				h.AttachmentPos = Vector3.new(0, -0.20, 0)
				wait(.1)
			elseif player:WaitForChild("leaderstats"):WaitForChild("Hats").Value == 2 then -- Box
				wait(1)
				local d = character:GetChildren()
				for i=1, #d do
					if (d[i].className == "Accessory") then
						d[i]:remove()
					end
				end
				if character:findFirstChild("Humanoid") ~= nil then -- Accessory script
					local h = Instance.new("Accessory")
					local p = Instance.new("Part")
					h.Name = "Hat"
					p.Parent = h
					p.Position = character:findFirstChild("Head").Position
					p.Name = "Handle"
					p.BrickColor = BrickColor.new("Really black") 
					p.formFactor = 0
					p.Size = Vector3.new(-0,-0,-1) 
					p.BottomSurface = 0
					p.TopSurface = 0
					p.Locked = true
					script.BoxMesh:Clone().Parent = p
					h.Parent = character
					print("It should work")
					h.AttachmentPos = Vector3.new(0, 0.15, -0.01) 
					wait(.1)
				elseif player:WaitForChild("leaderstats"):WaitForChild("Hats").Value == 3 then -- Business
					wait(1)
					local d = character:GetChildren()
					for i=1, #d do
						if (d[i].className == "Accessory") then
							d[i]:remove()
						end
					end
					if character:findFirstChild("Humanoid") ~= nil then -- Accessory script
						local h = Instance.new("Accessory")
						local p = Instance.new("Part")
						h.Name = "Hat"
						p.Parent = h
						p.Position = character:findFirstChild("Head").Position
						p.Name = "Handle"
						p.BrickColor = BrickColor.new("Really black") 
						p.formFactor = 0
						p.Size = Vector3.new(2, 0.8, 2)  
						p.BottomSurface = 0
						p.TopSurface = 0
						p.Locked = true
						script.BusinessMesh:Clone().Parent = p
						h.Parent = character
						print("It should work")
						h.AttachmentPos = Vector3.new(0, 0.15, -0.02) 
						wait(.1)
					elseif player:WaitForChild("leaderstats"):WaitForChild("Hats").Value == 4 then -- Curly
						wait(1)
						local d = character:GetChildren()
						for i=1, #d do
							if (d[i].className == "Accessory") then
								d[i]:remove()
							end
						end
						if character:findFirstChild("Humanoid") ~= nil then -- Accessory script
							local h = Instance.new("Accessory")
							local p = Instance.new("Part")
							h.Name = "Hat"
							p.Parent = h
							p.Position = character:findFirstChild("Head").Position
							p.Name = "Handle"
							p.BrickColor = BrickColor.new("Really black") 
							p.formFactor = 0
							p.Size = Vector3.new(1, 1, 1)   
							p.BottomSurface = 0
							p.TopSurface = 0
							p.Locked = true
							script.CurlyMesh:Clone().Parent = p
							h.Parent = character
							print("It should work")
							h.AttachmentPos = Vector3.new(0.03, 0.1, 0)
							wait(.1)
						elseif player:WaitForChild("leaderstats"):WaitForChild("Hats").Value == 5 then -- Dread
							wait(1)
							local d = character:GetChildren()
							for i=1, #d do
								if (d[i].className == "Accessory") then
									d[i]:remove()
								end
							end
							if character:findFirstChild("Humanoid") ~= nil then -- Accessory script
								local h = Instance.new("Accessory")
								local p = Instance.new("Part")
								h.Name = "Hat"
								p.Parent = h
								p.Position = character:findFirstChild("Head").Position
								p.Name = "Handle"
								p.BrickColor = BrickColor.new("Really black") 
								p.formFactor = 0
								p.Size = Vector3.new(2,2,2)
								p.BottomSurface = 0
								p.TopSurface = 0
								p.Locked = true
								script.DreadMesh:Clone().Parent = p
								h.Parent = character
								print("It should work")
			    				h.AttachmentPos = Vector3.new(0, 0.1, -0)
								wait(.1)
							elseif player:WaitForChild("leaderstats"):WaitForChild("Hats").Value == 6 then -- Emo
								wait(1)
								local d = character:GetChildren()
								for i=1, #d do
									if (d[i].className == "Accessory") then
										d[i]:remove()
									end
								end
								if character:findFirstChild("Humanoid") ~= nil then -- Accessory script
									local h = Instance.new("Accessory")
									local p = Instance.new("Part")
									h.Name = "Hat"
									p.Parent = h
									p.Position = character:findFirstChild("Head").Position
									p.Name = "Handle"
									p.BrickColor = BrickColor.new("Really black") 
									p.formFactor = 0
									p.Size = Vector3.new(2,2,2)
									p.BottomSurface = 0
									p.TopSurface = 0
									p.Locked = true
									script.EmoMesh:Clone().Parent = p
									h.Parent = character
									print("It should work")
									h.AttachmentPos = Vector3.new(0.1, 0.3, -0.1)
									wait(.1)
								elseif player:WaitForChild("leaderstats"):WaitForChild("Hats").Value == 7 then -- Faux"
									wait(1)
									local d = character:GetChildren()
									for i=1, #d do
										if (d[i].className == "Accessory") then
											d[i]:remove()
										end
									end
								if character:findFirstChild("Humanoid") ~= nil then -- Accessory script
									local h = Instance.new("Accessory")
									local p = Instance.new("Part")
									h.Name = "Hat"
									p.Parent = h
									p.Position = character:findFirstChild("Head").Position
									p.Name = "Handle"
									p.BrickColor = BrickColor.new("Really black") 
									p.formFactor = 0
									p.Size = Vector3.new(-0,-0,-1)  
									p.BottomSurface = 0
									p.TopSurface = 0
									p.Locked = true
									script.FauxMesh:Clone().Parent = p
									h.Parent = character
									print("It should work")
									h.AttachmentPos = Vector3.new(0,0.05,0.06)
									wait(.1)
								elseif player:WaitForChild("leaderstats"):WaitForChild("Hats").Value == 8 then -- Longdread
									wait(1)
									local d = character:GetChildren()
									for i=1, #d do
										if (d[i].className == "Accessory") then
											d[i]:remove()
										end
									end
									if character:findFirstChild("Humanoid") ~= nil then -- Accessory script
										local h = Instance.new("Accessory")
										local p = Instance.new("Part")
										h.Name = "Hat"
										p.Parent = h
										p.Position = character:findFirstChild("Head").Position
										p.Name = "Handle"
										p.BrickColor = BrickColor.new("Really black") 
										p.formFactor = 0
										p.Size = Vector3.new(1,1,1)  
										p.BottomSurface = 0
										p.TopSurface = 0
										p.Locked = true
										script.TreckyMesh:Clone().Parent = p
										h.Parent = character
										print("It should work")
										h.AttachmentPos = Vector3.new(0, 0.2, 0) 
										wait(.1)
									elseif player:WaitForChild("leaderstats"):WaitForChild("Hats").Value == 10 then -- Longdread
										wait(1)
										local d = character:GetChildren()
										for i=1, #d do
											if (d[i].className == "Accessory") then
												d[i]:remove()
											end
										end
									if character:findFirstChild("Humanoid") ~= nil then -- Accessory script
										local h = Instance.new("Accessory")
										local p = Instance.new("Part")
										h.Name = "Hat"
										p.Parent = h
										p.Position = character:findFirstChild("Head").Position
										p.Name = "Handle"
										p.BrickColor = BrickColor.new("Really black") 
										p.formFactor = 0
										p.Size = Vector3.new(2,2,2)   
										p.BottomSurface = 0
										p.TopSurface = 0
										p.Locked = true
										script.LongdreadMesh:Clone().Parent = p
										h.Parent = character
										print("It should work")
										h.AttachmentPos = Vector3.new(0, 0.2, 0) 
										wait(.1)
									elseif player:WaitForChild("leaderstats"):WaitForChild("Hats").Value == 9 then -- Longdread
										wait(1)
										local d = character:GetChildren()
										for i=1, #d do
											if (d[i].className == "Accessory") then
												d[i]:remove()
											end
										end
										if character:findFirstChild("Humanoid") ~= nil then -- Accessory script
											local h = Instance.new("Accessory")
											local p = Instance.new("Part")
											h.Name = "Hat"
											p.Parent = h
											p.Position = character:findFirstChild("Head").Position
											p.Name = "Handle"
											p.Material = Enum.Material.SmoothPlastic
											p.formFactor = 0
											p.Size = Vector3.new(-0,-0,-1)
											p.BottomSurface = 0
											p.TopSurface = 0
											p.Locked = true
											script.AfroMesh:Clone().Parent = p
											h.Parent = character
											print("It should work")
											h.AttachmentPos = Vector3.new(0, -0.20, 0)
											wait(.1)
										end
									end
								end
							end
						end
					end
				end
					end
				end
			end
		end
	end)
end)

Are you getting this weird bug where you can select and drag on text without it bugging out?

I just tested your script and I havent gotten anything from it, my main players hat value is 9 and im getting nothing.

Seems to only be when it’s = 1.

There’s a lot of repeating in your code. The problem with the elseif’s is that you were testing the conditions only when the value of Hats(assuming it was a value object) == 1:

if player:WaitForChild("leaderstats"):WaitForChild("Hats").Value == 1 then -- Afro

Based on your intentions, your elseif blocks are placed in the wrong places. What you are doing is essentially checking if the value of hats is 1 and then if it isn’t, your code does not check for other cases like you want it to. To fix that, you would move your elseifs to match the outermost if-else block. For example:

if player:WaitForChild("leaderstats"):WaitForChild("Hats").Value == 1 then -- Afro
    -- code to do when case is 1
elseif player:WaitForChild("leaderstats"):WaitForChild("Hats").Value == 2 then
    -- code to do when case is 2
end

I would also index both the hats into a variable like this:

local hatsValue = player:WaitForChild("leaderstats"):WaitForChild("Hats").Value

Also if I’m not mistaken, there is a RemoveAccessories() method on the character’s humanoid. So you should be able to do this instead of having a for loop remove the accessories:

local humanoid = character:FindFirstChild("Humanoid")
if humanoid then humanoid:RemoveAccessories() end