Trying to subtract from an Attribute in Workspace

Yeah, this scripting looks really bad, and I apologize for that, I just learned how to properly script a few days ago.

I’m trying to have an energy percentage system akin to FNaF. The percentage value is stored in an Attribute in Workspace, but for some reason, my script won’t alter the value at all.

I’ve tried having the Attribute somewhere else, but that didn’t work. I probably did something wrong, but I don’t know what.

local light = workspace.LightL.Light.PointLight
local switch = false
local fog = workspace.LightL.Fog
local fog2 = workspace.LightL.ThickFog
local poweron = script.Parent.Parent.Enable
local poweroff = script.Parent.Parent.Disable
local energy = workspace:GetAttribute("Energy")

local function onClicked(player)
    if switch == false then
        light.Enabled = true
        poweron:Play()
        for i, v in pairs(fog:GetDescendants()) do
            if v:IsA("Part") then
                v.Transparency = 1
            end
        end
        fog2.Transparency = 1
        print "it worked!!"
        switch = true
        else
        light.Enabled = false
        poweroff:Play()
        for i, v in pairs(fog:GetDescendants()) do
            if v:IsA("Part") then
                v.Transparency = 0.8
            end
        end
        fog2.Transparency = 0
        print "it's on!!!"
        switch = false
    end
end

while switch do
    workspace:SetAttribute("Energy",workspace:GetAttribute("Energy") - 1)
    wait (0.2)
end

click.MouseClick:Connect(onClicked)```

This script is supposed to activate a light and remove darkness, and while the switch is on, power is drained, but power never drains.
local newEnergy = 100

while switch do
    newEnergy -= 1
    workspace:SetAttribute("Energy", newEnergy)
    wait(0.2)
end

How about something like this …

click.MouseClick:Connect(onClicked)

while switch do
    energy = workspace:GetAttribute("Energy")
    workspace:SetAttribute("Energy", energy - 1)
    wait(0.2)
end

It’s not going to make to the click line the way you intended with the while loop over it like that.

1 Like

I’ll explain your issue.

“switch” was set to false, so the while loop never ran. And when you fired the “onClicked” function, you set “switch” to true, but you’d need to make a new while loop for that, since the one you put already ran when “switch” was false, and therefore nothing happened.

Instead, you could try a different approach:

local light = workspace.LightL.Light.PointLight
local switch = false
local fog = workspace.LightL.Fog
local fog2 = workspace.LightL.ThickFog
local poweron = script.Parent.Parent.Enable
local poweroff = script.Parent.Parent.Disable
local energy = workspace:GetAttribute("Energy")

local function onClicked(player)
    if switch == false then
        light.Enabled = true
        poweron:Play()
        for i, v in pairs(fog:GetDescendants()) do
            if v:IsA("Part") then
                v.Transparency = 1
            end
        end
        fog2.Transparency = 1
        print "it worked!!"
        switch = true

        while switch do
            workspace:SetAttribute("Energy",workspace:GetAttribute("Energy") - 1)
            wait (0.2)
        end
    else
        light.Enabled = false
        poweroff:Play()
        for i, v in pairs(fog:GetDescendants()) do
            if v:IsA("Part") then
                v.Transparency = 0.8
            end
        end
        fog2.Transparency = 0
        print "it's on!!!"
        switch = false
    end
end

click.MouseClick:Connect(onClicked)

This way, you’re creating a new while loop once you press the button to turn the “switch” variable to true. Then, once you press it again, the “switch” variable is set to false, turning off the while loop.

1 Like

After looking at this again a bit deeper …
This line: workspace:SetAttribute(“Energy”,workspace:GetAttribute(“Energy”) - 1)
is formatted incorrectly. You need the part (obj) the attribute is on …

working test
local switch = true
local part = workspace:WaitForChild("Part")

while switch do
	part:SetAttribute("Energy",part:GetAttribute("Energy") - 1)
	task.wait(1)
end

note: Made a part and placed it on the workspace. Then added an attribute to that part named Energy as a number. I set that to 1000. Ran the test and then watched as it counted down the Energy attribute on that part from the studio’s explorer window. Simple format mistake … good luck!

The easiest solution would be to change:

while switch do
    workspace:SetAttribute("Energy",workspace:GetAttribute("Energy") - 1)
    wait (0.2)
end

to:

while true do
   if switch then workspace:SetAttribute("Energy", workspace:GetAttribute("Energy") - 1) end
   task.wait(0.2)
end

The problem is doing while switch do will only make the loop check the switch value once so it will only run if it happens to be true already

Edit: @pkalttestacc1 Also move this line:
click.MouseClick:Connect(onClicked)
before the loop else it won’t activate because the loop will block it, like this is what you’ll need to do:

click.MouseClick:Connect(onClicked)

while true do
   if switch then workspace:SetAttribute("Energy", workspace:GetAttribute("Energy") - 1) end
   task.wait(0.2)
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.