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

DEFCON indicator not changing brick color?

Before i begin, I will warn you all that i do not know very much lua, so please forgive me if my issue is something stupidly obvious.

The basic idea of my script is to change the material of 5 bricks according to the NumberValue (Named DefconLevel), Except when 'DefconLevel.Value' Is changed, None of the materials change. (I only have the print in defcon 3 because that was the one i was switching to for testing.)

print("Defcon script started")
defcon = script.Parent.Defconlevel.Value
p = script.Parent
while true do
	if defcon == 1 then
		p.defcon1.Material = "Neon"
		p.defcon2.Material = "Plastic"
		p.defcon3.Material = "Plastic"
		p.defcon4.Material = "Plastic"
		p.defcon5.Material = "Plastic"
	elseif defcon == 2 then
		p.defcon1.Material = "Plastic"
		p.defcon2.Material = "Neon"
		p.defcon3.Material = "Plastic"
		p.defcon4.Material = "Plastic"
		p.defcon5.Material = "Plastic"
	elseif defcon == 3 then
		p.defcon1.Material = "Plastic"
		p.defcon2.Material = "Plastic"
		p.defcon3.Material = "Neon"
		p.defcon4.Material = "Plastic"
		p.defcon5.Material = "Plastic"
		print("defcon level is now 3!")
	elseif defcon == 4 then
		p.defcon1.Material = "Plastic"
		p.defcon2.Material = "Plastic"
		p.defcon3.Material = "Plastic"
		p.defcon4.Material = "Neon"
		p.defcon5.Material = "Plastic"
	elseif defcon == 5 then
		p.defcon1.Material = "Plastic"
		p.defcon2.Material = "Plastic"
		p.defcon3.Material = "Plastic"
		p.defcon4.Material = "Plastic"
		p.defcon5.Material = "Neon"
	end
	wait(0.1)
	warn ("checked")
end

2 comments

0

Material takes an Enum not a string

0

Also, you should be using local variables

1 answer

GENERAL PRACTICE

Use :WaitForChild() to make sure a part exists before changing its properties or values

Use the Material Enuminstead of a string since even if the string works it will glitch and not work for every player, while the Enum is the intended method to change it.

ISSUES

You initially define "defcon" as a certain value, however, since this is at the start of the script, this variable will remain the same no matter what you do to it

To solve this, place the variable inside the while-loop and every time the loop runs, it will check the value, instead of only when the script was created.

REVISED SCRIPT

print("Defcon script started")

local p = script.Parent
local defcon1 = p:WaitForChild("defcon1")
local defcon2 = p:WaitForChild("defcon2")
local defcon3 = p:WaitForChild("defcon3")
local defcon4 = p:WaitForChild("defcon4")
local defcon5 = p:WaitForChild("defcon5")

while true do
	local defcon = p:WaitForChild("Defconlevel").Value
	if defcon == 1 then
		defcon1.Material = Enum.Material.Neon
		defcon2.Material = Enum.Material.Plastic
		defcon3.Material = Enum.Material.Plastic
		defcon4.Material = Enum.Material.Plastic
		defcon5.Material = Enum.Material.Plastic
	elseif defcon == 2 then
		defcon1.Material = Enum.Material.Plastic
		defcon2.Material = Enum.Material.Neon
		defcon3.Material = Enum.Material.Plastic
		defcon4.Material = Enum.Material.Plastic
		defcon5.Material = Enum.Material.Plastic
	elseif defcon == 3 then
		defcon1.Material = Enum.Material.Plastic
		defcon2.Material = Enum.Material.Plastic
		defcon3.Material = Enum.Material.Neon
		defcon4.Material = Enum.Material.Plastic
		defcon5.Material = Enum.Material.Plastic
		print("defcon level is now 3!")
	elseif defcon == 4 then
		defcon1.Material = Enum.Material.Plastic
		defcon2.Material = Enum.Material.Plastic
		defcon3.Material = Enum.Material.Plastic
		defcon4.Material = Enum.Material.Neon
		defcon5.Material = Enum.Material.Plastic
	elseif defcon == 5 then
		defcon1.Material = Enum.Material.Plastic
		defcon2.Material = Enum.Material.Plastic
		defcon3.Material = Enum.Material.Plastic
		defcon4.Material = Enum.Material.Plastic
		defcon5.Material = Enum.Material.Neon
	end
	wait(0.1)
	print("checked")
end

3 comments

1

just why?

0

It's still not working. The while loop works, but when the value is changed, nothing happens to the lights.

0

You are using a Server Script right? That or make sure whichever script is altering the "defcon" value is changing this on the server

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