Script not working without an error

Hey there! I am new to scripting! I just tried to make a script that changes a brick’s material on a click of a GUI. No errors show, but it is still not working.

My Script:

local Light = game.Workspace.LightYes
local Button = script.Parent

function onClick()
	if Light.Material == "256" then
		Light.Material = "288"
		print ("Light = Neon")
		
		elseif Light.Material == "288" then
			Light.Material = "256"
			print ("Light = Plastic")
	end
end

script.Parent.MouseButton1Click:connect(onClick)

Thanks for the help!

Instead of the numbers, you can use something like Enum.Material.Neon or Enum.Material.Plastic to change materials.

1 Like

Materials are not numbers:

if Light.Material == "256" then

Materials are strings:

if Light.Material == "Neon" then

Thanks! That really does help a lot. I hate using numbers for materials.

Like others have said, use Enum.Material. The issue is that Lua won’t coerce strings to numbers (that means 1 == "1" will always be false). This is the reason no errors were thrown yet nothing was occuring.

Light.Enum.Material.Plastic

Good?

I believe it would just be Enum.Material.Plastic

Refer to the material enum here: Material | Documentation - Roblox Creator Hub

This shows the integer values associated within the enumeration of Material.

Materials are actually Enums. They have a name and a value. When you write to BasePart.Material, the input is converted into its respective Enum. When setting it to a number, it will find the Material Enum with the matching value. Same with string, except if the Name matches the string.

Setting a BasePart’s material to “Neon” or “288” will convert it to Enum.Material.Neon.

2 Likes