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 a brick that it's material is randomized whenever someone enters to the game?

I tried making a part with Instance.new. I got it's color, but not the material. The color and the material is both supposed to be random, but the material part has a error saying Invalid value number of the material in math.random for enum Material! Please help. (The area in code that is not working starts from part.Material = math.random (1, 21) do...end )

function onClick(click) 
	wait(0.05)
local part = Instance.new('Part')
part.Anchored = true
part.Parent = workspace
part.Name = "NewPart"
part.Color = Color3.new(math.random(), math.random(), math.random())
part.Material = math.random(1, 21) do
	if part.Material == 1 then 
		part.Material = "Plastic"
	end
		if part.Material == 2 then 
		part.Material = "Wood"
	end
		if part.Material == 3 then 
		part.Material = "Wood Planks"
	end
		if part.Material == 4 then 
		part.Material = "Slate"
	end
		if part.Material == 5 then 
		part.Material = "Concrete"
	end
		if part.Material == 6 then 
		part.Material = "Metal"
	end
		if part.Material == 7 then 
		part.Material = "Corroded Metal"
	end
		if part.Material == 8 then 
		part.Material = "Diamond Plate"
	end
		if part.Material == 9 then 
		part.Material = "Foil"
	end
		if part.Material == 10 then 
		part.Material = "Grass"
	end
		if part.Material == 11 then 
		part.Material = "Ice"
	end
		if part.Material == 12 then 
		part.Material = "Brick"
	end
		if part.Material == 13 then 
		part.Material = "Sand"
	end
		if part.Material == 14 then 
		part.Material = "Fabric"
	end
		if part.Material == 15 then 
		part.Material = "Granite"
	end
		if part.Material == 16 then 
		part.Material = "Marble"
	end
		if part.Material == 17 then 
		part.Material = "Pebble"
	end
		if part.Material == 18 then 
		part.Material = "Cobblestone"
	end
		if part.Material == 19 then 
		part.Material = "SmoothPlastic"
	end
		if part.Material == 20 then 
		part.Material = "Neon"
	end
		if part.Material == 21 then 
		part.Material = "Glass"
	end
end
part.Position = Vector3.new(-1.582, 6.037, 8.592)
workspace.check.Checker.SurfaceGui.TextLabel.Text = "Part exists, to create different one; destroy it and create a new one."
script.Parent.Disabled = true
end


script.Parent.MouseClick:connect(onClick)

1 comment

0

Enum.Material, that's the proper way to get a material. But, some of the Materials are terrain-based

2 answers

At line 8 you were setting the Material to a random number between 1 and 21, and neither of those is a valid number for material.

Also, you dont need that many if statements, you can use the method called GetEnumItems()

function onClick(click) 
	wait(0.05)
	local part = Instance.new('Part')
	part.Anchored = true
	part.Parent = workspace
	part.Name = "NewPart"
	part.Color = Color3.new(math.random(), math.random(), math.random())
	local materials = Enum.Material:GetEnumItems()
	part.Material = materials[math.random(1, #materials)]
	part.Position = Vector3.new(-1.582, 6.037, 8.592)
	workspace.check.Checker.SurfaceGui.TextLabel.Text = "Part exists, to create different one; 	destroy it and create a new one."
	script.Parent.Disabled = true
end


script.Parent.MouseClick:connect(onClick)

well I guess you could do this

local part = Instance.new("Part") part.Parent = workspace
while wait(.5) do
part.Material = math.random(16,18)*16 wait(.5)
part.Material = math.random(32,33)*16 wait(.5)
part.Material = math.random(65,68)*16
end

1 comment

0

xdd

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