I’ve been trying to create the system where if you start running your energy decreases continuously until you stop running.
My problem is that the loop which causes the energy value to decrease keeps on going even after you stop running. It also does actually break for like a millisecond or so, but then apparently the entire loop repeats right after breaking, causing the loop to keep on decreasing the energy value.
Here is the server script in which I’m actually having problems:
local runService = game:GetService("RunService")
local changeAmount = .125
script.Parent:WaitForChild("ChangeEnergyRun").OnServerEvent:Connect(function(plr, action)
local char = plr.Character
local energy = plr:FindFirstChild("Energy")
if action == "Start" then
if char:FindFirstChild("Decreasing") then
else
local decreasing = Instance.new("BoolValue")
decreasing.Name = "Decreasing"
decreasing.Parent = char
end
if action == "Stop" then
if char:FindFirstChild("Decreasing") then
char:FindFirstChild("Decreasing"):Destroy()
end
end
end
spawn(function()
while runService.Heartbeat:Wait() do
if action == "Start" then
energy.Value -= changeAmount
elseif action == "Stop" then
break
end
end
end)
end)
I would really appreciate some help on this as I’ve spent too long trying to fix this issue myself.
local decreasingCo = coroutine.create(function()
while runService.Heartbeat:Wait() do
if action == "Start" then
energy.Value -= changeAmount
end
end
end)
and replace that with this
if action == "Stop" then
if decreasingCo then
coroutine.yield(decreasingCo)
end
end
and a coroutine is basically like spawn but you can do a loop and pause/yield it and start it again whenever you want to (thats the best way i can explain it lol)
Because action is created every time the event fires. Try this
local runService = game:GetService("RunService")
local changeAmount = .125
local updateThread;
script.Parent:WaitForChild("ChangeEnergyRun").OnServerEvent:Connect(function(plr, action)
local char = plr.Character
local energy = plr:FindFirstChild("Energy")
if action == "Start" then
if char:FindFirstChild("Decreasing") then
else
local decreasing = Instance.new("BoolValue")
decreasing.Name = "Decreasing"
decreasing.Parent = char
end
if action == "Stop" then
if char:FindFirstChild("Decreasing") then
char:FindFirstChild("Decreasing"):Destroy()
end
end
end
if not updateThread and action == "Start" then
updateThread = task.spawn(function()
while runService.Heartbeat:Wait() do
energy.Value -= changeAmount
end
end)
elseif action == "Stop" then
task.cancel(updateThread)
end
end)
local runService = game:GetService("RunService")
local changeAmount = .125
local updateThread;
script.Parent:WaitForChild("ChangeEnergyRun").OnServerEvent:Connect(function(plr, action)
local char = plr.Character
local energy = plr:FindFirstChild("Energy")
if action == "Start" then
if char:FindFirstChild("Decreasing") then
else
local decreasing = Instance.new("BoolValue")
decreasing.Name = "Decreasing"
decreasing.Parent = char
end
if action == "Stop" then
if char:FindFirstChild("Decreasing") then
char:FindFirstChild("Decreasing"):Destroy()
end
end
end
if not updateThread and action == "Start" then
updateThread = task.spawn(function()
while runService.Heartbeat:Wait() do
energy.Value -= changeAmount
end
end)
elseif updateThread and action == "Stop" then
task.cancel(updateThread)
end
end)
local runService = game:GetService("RunService")
local changeAmount = .125
local updateThread;
script.Parent:WaitForChild("ChangeEnergyRun").OnServerEvent:Connect(function(plr, action)
local char = plr.Character
local energy = plr:FindFirstChild("Energy")
if action == "Start" then
if char:FindFirstChild("Decreasing") then
else
local decreasing = Instance.new("BoolValue")
decreasing.Name = "Decreasing"
decreasing.Parent = char
end
end
if action == "Stop" then
if char:FindFirstChild("Decreasing") then
char:FindFirstChild("Decreasing"):Destroy()
end
end
if not updateThread and action == "Start" then
updateThread = task.spawn(function()
while runService.Heartbeat:Wait() do
energy.Value -= changeAmount
end
end)
elseif updateThread and action == "Stop" then
task.cancel(updateThread)
end
end)
Ahh mb, I forgot threads don’t get set to nil automatically
local runService = game:GetService("RunService")
local changeAmount = .125
local updateThread;
script.Parent:WaitForChild("ChangeEnergyRun").OnServerEvent:Connect(function(plr, action)
local char = plr.Character
local energy = plr:FindFirstChild("Energy")
if action == "Start" then
if char:FindFirstChild("Decreasing") then
else
local decreasing = Instance.new("BoolValue")
decreasing.Name = "Decreasing"
decreasing.Parent = char
end
end
if action == "Stop" then
if char:FindFirstChild("Decreasing") then
char:FindFirstChild("Decreasing"):Destroy()
end
end
if not updateThread and action == "Start" then
updateThread = task.spawn(function()
while runService.Heartbeat:Wait() do
energy.Value -= changeAmount
end
end)
elseif updateThread and action == "Stop" then
task.cancel(updateThread)
updateThread = nil
end
end)