Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

DEFCON indicator not changing brick color?

Asked by 5 years ago

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
0
Material takes an Enum not a string DeceptiveCaster 3761 — 5y
0
Also, you should be using local variables DeceptiveCaster 3761 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

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
1
just why? User#5423 17 — 5y
0
It's still not working. The while loop works, but when the value is changed, nothing happens to the lights. TigerManGamingYT 2 — 5y
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 SerpentineKing 3885 — 5y
Ad

Answer this question