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

Why does this return true?

Asked by
ausmel105 140
8 years ago

So the following code is supposed to create functioning indicators, but my output was telling me I was giving the "Material" property an invalid enum value for material.

I have tried using a ternary operator, but instead of returning "SmoothPlastic" when state = true, it simply returns true. However in the first running of this loop, when state = false.. my indicators are infact set to neon.

Why is it doing this? I have used ternary operators before and it has not behaved like this, am I missing something? My code is as follows:

local state = false
while indicate == 2 do
    for i, v in pairs (script.Parent.Parent.Parent.Indicators:GetChildren()) do
        if v.Name == "IndicatorR" then
            local mat = state or "SmoothPlastic" and "Neon"
            print(mat)
            -- For demonstration purposes.
            v.Material = state or "SmoothPlastic" and "Neon"
        end
    end
    script.Parent.Indicator:Play()
    wait(0.5)
    state = not state
end

As always, I appreciate your time and efforts. :)

1 answer

Log in to vote
2
Answered by 8 years ago

Solution

The solution would simply be inverting your logic gates (assuming I understood your problem correctly). For example, you want to get the "SmoothPlastic" value when "state" is true.

Therefore, both "state" and "SmoothPlastic" must be true (and obviously "SmoothPlastic" will always be true, since it's neither nil nor false). So in that case, you only need state to be true, to give you "SmoothPlastic"

local state = false
local inds = script.Parent.Parent.Parent:WaitForChild("Indicators") -- WaitForChild never hurts

while indicate == 2 do
    for i, v in pairs (inds:GetChildren()) do
        if v.Name == "IndicatorR" then

            -- Inverted logic gates
            local mat = state and "SmoothPlastic" or "Neon" 
            v.Material = mat
            print(mat)

        end
    end
    script.Parent.Indicator:Play()
    wait(0.5)
    state = not state
end

Hope I understood you correctly, sorry if you were asking something else (still haven't slept in two days).

External help

If this was your problem and you'd like to know more about how logical operators work, I've put together a video that I'm fairly proud of that you may or may not find useful: https://www.youtube.com/watch?v=AXD2NaCvKdc

Anyway, hope it helped.

0
Thank you, video was helpful too! ausmel105 140 — 8y
0
Cool, glad it helped :D ScriptGuider 5640 — 8y
0
+1 for excellent video. Link150 1355 — 8y
Ad

Answer this question