For this example, I have a fishing system in my game. The flow is like this:
Player Clicks on Fishing Spot → Sends Request to Server → Server Begins Fishing Loop
The loop is like this
function Player:beginFishing()
self.state = States.Fishing
task.spawn(function()
while (self.state == States.Fishing) do
task.wait(math.random(3,5))
-- Catch fish
end
end)
end
The problem: If the user stops fishing (their state goes from Fishing to None) and then clicks on the fishing spot again within the time that wait(X)
is yielding, another loop will spawn because the first loop has a wait
in it before the while loop’s condition is checked again.
So when the while loop checks again, the user is set to Fishing state again. So both loops keep running.
I have a lot of skills that use a loop system like this. So I am looking for suggestions on a scalable method to handle this issue.