• Loop problem
    7 replies, posted
Hi, I'm having an issue. The problem is that when I hit the timer "name1", it does what it supposed to do but the timer "stop1" doesn't work anymore. I tried with timer.Stop but I have the same issue. Could anyone helps me ? Thanks for your help lads ! local dmg1 = 1 local chance1 = 2 local time1 = 10 local timedelay1 = 1 util.AddNetworkString( "name1" ) net.Receive( "name1", function(len, ply, amount, self) timer.Create("stop1", timedelay1, 0, function() x = math.random( 1, chance1 ) print(x.."n1") if x == 2 and ply:Alive() then ply:SendLua("GAMEMODE:AddNotify(\"n1.\", NOTIFY_GENERIC, 5)") timer.Create("name1", 1, time1, function() if ply:Alive() then ply:SetHealth(ply:Health()-dmg1) timer.Remove("stop1") --timer.Stop("stop1") else timer.Remove("name1") end end) end end ) --timer end ) --function
This appears to work fine if I understand the intention correctly- after a couple of seconds the random condition is met and the client is shown a notification, before 1 health point is taken every second for 10 seconds. When you say "stop1" doesn't work anymore, can you clarify what you mean?
I wanted to make a function which when I get a random number (condition) it takes 1 health point every second for 10 seconds. This function should work every "timedelay1" but when the condition is true, it takes 1 health point every second for 10 seconds but the timer "stop1" that I used to make the function every "timedelay1" doesn't work anymore.
Proper indentation would help you follow your code's flow a lot easier local dmg1 = 1 local chance1 = 2  local time1 = 10 local timedelay1 = 1 util.AddNetworkString( "name1" ) net.Receive( "name1", function(len, ply, amount, self) timer.Create("stop1", timedelay1, 0, function() x = math.random( 1, chance1 ) print(x.."n1")          if x == 2 and ply:Alive() then ply:SendLua("GAMEMODE:AddNotify(\"n1.\", NOTIFY_GENERIC, 5)") timer.Create("name1", 1, time1, function() if ply:Alive() then ply:SetHealth(ply:Health()-dmg1) timer.Remove("stop1") --timer.Stop("stop1") else timer.Remove("name1") end end) end end ) --timer end ) --function You're probably running into the issue because you're stopping the "stop1" timer right below you take 1 health away from the player. This will keep stopping the timer each second until the player dies. If I understand you correctly you want to call timer.Remove("stop1") right above your SendLua instead: local dmg1 = 1 local chance1 = 2  local time1 = 10 local timedelay1 = 1 util.AddNetworkString( "name1" ) net.Receive( "name1", function(len, ply, amount, self) timer.Create("stop1", timedelay1, 0, function() x = math.random( 1, chance1 ) print(x.."n1")          if x == 2 and ply:Alive() then timer.Remove("stop1") ply:SendLua("GAMEMODE:AddNotify(\"n1.\", NOTIFY_GENERIC, 5)") timer.Create("name1", 1, time1, function() if ply:Alive() then ply:SetHealth(ply:Health()-dmg1) else timer.Remove("name1") end end) end end ) --timer end ) --function
My explanations were probably bad . I would to create a "disease system" where every X seconds, there is 1/Y chance to become ill and lose Z hp during a time T. Moreover I would like to have 0 chance to get ill one more time when i'm already ill, ( Y=0 while I lose Z hp during T).
So you create a timer that triggers a function that gets all players and for 1/Y change, you insert them into a "disease" table which holds every diseased players, but check if the player is already on the list. Then you create another timer which triggers every T seconds which triggers a function that decreases hp per call.
It's the right idea, but it won't work. Timers should be created after function definitions because right now you're giving them a nil value for a function. It also won't handle if a player leaves the server while ill (will cause errors), if a player dies (they won't be cleared from sickness), and it won't stop their illness after a certain amount of ticks. This one (should) work out of the box. It's untested though. -- Every 10 seconds, everyone has a chance to become ill local diseaseChanceInterval = 10 -- A player has a 50% chance of becoming ill local diseaseChance = 50 -- When ill, a player will take 10 ticks of damage local damageTicks = 10 -- When ill, a player will take 1 damage per tick local damagePerTick = 1 local sickPlayers = {} timer.Create("ContractDiseases", diseaseChanceInterval, 0, function() for k, v in ipairs(player.GetAll()) do if (sickPlayers[v]) then continue end -- If the player is already sick, we don't want them to become sick again yet if (math.random(100) <= diseaseChance) then sickPlayers[v] = damageTicks end end end) timer.Create("DealDiseaseDamage", 1, 0, function() for k, v in pairs(sickPlayers) do if (!IsValid(k)) then -- The player probably left the server sickPlayers[k] = nil continue end k:TakeDamage(damagePerTick) sickPlayers[k] = sickPlayers[k] - 1 if (!k:Alive( or v <= 0)) then -- The player either died or took the full amount of damage from disease. They're no longer sick sickPlayers[k] = nil end end end)
Hopefully god sent you to me ! Thank you Einstein !
Sorry, you need to Log In to post a reply to this thread.