I assigned a coroutine wrapping my function to a variable that resumes whenever a tool is activated. I put the innards of the function in a while loop advised by https://scriptinghelpers.org/questions/22442/coroutine-error-whats-wrong to keep the thread from dying. The thread, however, exhausts execution time and freezes the game despite having a wait in the while loop.
local proximityChanges = coroutine.create( function(targetPart)
while true do
print("While True")
repeat
local distance = (tip.Position - targetPart.Position).Magnitude
if distance > 150 then
tweenColor(tip, Color3.new(1, 1, 1), 1)
proximity:Play() -- sound
wait(delayChange)
elseif distance < 100 then
tweenColor(tip, Color3.new(1, 0.94902, 0.219608), 1)
proximity:Play()
wait(delayChange)
elseif distance < 60 then
tweenColor(tip, Color3.new(1, 0.52549, 0.207843), 1)
proximity:Play()
wait(delayChange)
elseif distance < 40 then
tweenColor(tip, Color3.new(1, 0.447059, 0.34902), 1)
proximity:Play()
wait(delayChange)
elseif distance < 15 then
tweenColor(tip, Color3.new(1, 0.172549, 0.145098), 1)
proximity:Play()
wait(delayChange)
end
print(distance)
until powered == false
coroutine.yield() -- if the item is not powered, stop execution
wait(delayChange) -- exhausts unless waiting. maybe a delay between yield/resume?
end
end)
the repeat loop doesn’t have a wait(), well it does, but inside a if statement, if distance = 120, it won’t fulfill your if statements, sooo, no wait()
Thank you. I’ve updated the code, however Resumed!, While True, and Repeat are printed once; after the thread yields, it appears to die and not allow resuming anymore. The if statements don’t catch the distance either, nor the else.
It’s unrelated, but breakpoints seem to not work on coroutines
local proximityChanges = coroutine.create( function(targetPart)
print("\tResumed!")
while true do
print("While True")
repeat
print("Repeat")
local distance = (tip.Position - targetPart.Position).Magnitude
if distance >= 150 then -- 150 +
tweenColor(tip, Color3.new(1, 1, 1), 1)
proximity:Play()
wait(delayChange)
elseif 150 > distance >= 100 then -- 149 -> 100
tweenColor(tip, Color3.new(1, 0.94902, 0.219608), 1)
proximity:Play()
wait(delayChange)
elseif 100 > distance >= 60 then -- 99 -> 60
tweenColor(tip, Color3.new(1, 0.52549, 0.207843), 1)
proximity:Play()
wait(delayChange)
elseif 60 > distance >= 40 then -- 59 -> 40
tweenColor(tip, Color3.new(1, 0.447059, 0.34902), 1)
proximity:Play()
wait(delayChange)
elseif 40 > distance >= 0 then -- 40 -> 0
tweenColor(tip, Color3.new(1, 0.172549, 0.145098), 1)
proximity:Play()
wait(delayChange)
else
print("Distance not in boundaries: " .. tostring(distance))
end
print(distance)
until powered == false
coroutine.yield()
wait(delayChange) -- exhausts unless waiting. maybe a delay between yield/resume?
end
end)
if, let’s say there was a negative distance, the else case would happen, the repeat loop would not yield, it would start a new iteration, distance did not change, it is still the same negative number (the thread did not yield) so the else case again occurs. And so on
if the else case occurs once, it will happen forever.