#Can someone review my script? It's about energy draining on part click.

1 messages · Page 1 of 1 (latest)

sweet lantern
#

gycort = 100
local powerG = workspace.powerG
local isDraining = false

local clickDetector = Instance.new("ClickDetector", powerG)

local function updatePowerG()
powerG.CanCollide = energycort <= 0
powerG.Transparency = energycort > 0 and 0.5 or 0
powerG.Color = energycort > 0 and Color3.new(0,1,0) or Color3.new(1,0,0)
end

clickDetector.MouseClick:Connect(function()
isDraining = not isDraining

while isDraining and energycort > 0 do
    energycort = energycort - 1
    updatePowerG()
    wait(1)
end

if energycort <= 0 then isDraining = false end

end)

powerG.Anchored = true
updatePowerG()
Expected Behavior:
First click → starts draining 1 energy per second (turns green/transparent)

Second click → stops draining

At 0 energy → part turns red/solid and stops automatically

Actual Problem:
❗ [Describe your specific issue here in English]. For example:

The energy drains but the part doesn't change color

ClickDetector works inconsistently

I get error "[paste error here]" in Output

Energy resets unexpectedly

What I've Tried:
Made sure powerG exists in Workspace

Confirmed ClickDetector is attached to the part

Added print("Energy:", energycort) to debug values

Additional Info:
Roblox Studio version: [your version]

Testing in: Play Solo/Published

Error messages: [paste any red errors from Output]

Questions:

Is my energy drain loop implementation correct?

Should I use RunService instead of wait()?

How can I make this more efficient?

P.S. For visual reference:

🔴 Red = No energy (solid)

🟢 Green = Energy available (transparent)

Let me know if you need more details! Thanks in advance! 🙏

vague spade