Basic script to have a decreasing power variable won't work

I’m still working on my project and I want to add a decreasing power variable to the power station in it, so every once in a while someone has to go over and push a button to get the power back… problem is, my script won’t make the power variable decrease; it just stays at 1.
I’m not sure what I did wrong, anyone know what’s wrong with my script?

Firstly, you have some syntax errors going on with writing variable names incorrectly

Secondly, you have 2 while true do loops, so the first one will work, but the second wont

Thirdly, you forgot to put .Value when referencing the value in Power

Can you copy and paste the code ehre so I can try to help out in fixing the code?

Okay.

local off = script.Parent.Off
local LO = script.Parent.LightO
local LOF = script.Parent.LightOF
script.Parent.ClickDetector.MouseClick:connect(onClicked)

function onClicked()
	script.Parent.Sound:Play()
	script.Parent.Power = 1
end

while true do
	if script.Parent.Power > 0 then
		repeat
			script.Parent.Power = script.Parent.Power - 0.01
			wait(6)
		until script.Parent.Power <= 0
	end
end

while true do
	if script.Parent.Power == 0 then
		script.Parent.BrickColor = Off
		script.Parent.SurfaceLight.Color = LOF
	else
		script.Parent.BrickColor = On
		script.Parent.SurfaceLight.Color = LO
	end
end


Something like this should do the trick

local off = script.Parent.Off
local on = script.Parent.On
local LO = script.Parent.LightO
local LOF = script.Parent.LightOF
local power = script.Parent.Power
script.Parent.ClickDetector.MouseClick:connect(onClicked)

function onClicked()
	script.Parent.Sound:Play()
	power.Value = 1
end

power.Changed:Connect(function()
	if power.Value == 0 then
		script.Parent.BrickColor = off.Value
		script.Parent.SurfaceLight.Color = LOF.Value
	else
		script.Parent.BrickColor = on.Value
		script.Parent.SurfaceLight.Color = LO.Value
	end
end)

coroutine.wrap(function()
	while true do
		if power.Value > 0 then
			repeat
				script.Parent.Power -= 0.01
				wait(6)
			until power.Value <= 0
		end
	end
end)()

The 2nd while loop was unnecessary when you could just do .Changed on the power value

1 Like

Ah thanks. Didn’t realize it wasn’t properly syntaxed, the code made enough sense to me…

1 Like

It’s okay! Least you know the issues relating to your code now! If you have anymore issues don’t be afraid to make another post!

Yeah, I won’t be afraid to. In case it isn’t obvious enough, scripting isn’t my strong point. I waited 10 minutes for it to finish and sure enough the script worked for me at least

1 Like