Improving a script that randomizes an NPC's accessory

Hey Devforum! So I made a pretty basic script that can randomize an NPC’s appearance (accessories and torso color).

local randomHat = math.random(0, 2)
local randomTorso = math.random(0, 1)


if (randomHat == 0) then
	script.Parent.visor:Destroy()
else if (randomHat == 1) then
		script.Parent.cap:Destroy()
	else
		script.Parent.visor:Destroy()
		script.Parent.cap:Destroy()
	end
end



if (randomTorso == 0) then
	script.Parent.Torso.Color = Color3.fromRGB(13, 105, 172); -- blue
else
	script.Parent.Torso.Color = Color3.fromRGB(196, 40, 28);  -- red
end

I’m trying to figure out an efficient way for the code to randomize accessories instead of having the NPC wear all possible hats, and then deleting certain ones based on the output. What should I look for?
styleRandomizer.rbxl (46.9 KB)

1 Like

It looks very nice and better then mine lol!

1 Like

Why not just equip only one based on the randomly generated number’s result?

1 Like

I’m still new to ServerStorage, but I figure that I should try using it and have the code clone hats stored underneath it and equip them to the NPC.