Hello everyone. I have two questions related to the operation of the function Task.Wait()
- I wrote a script for a sprint. When you click on the shift, the player runs and stamina is spent. It works, but recently I decided to check my endurance by a stopwatch. Here is my piece of code:
function OnBindingPressed(player, bindingPressed) if bindingPressed == shiftKeyBinding then sprintTable[player] = true end end
function CheckForStaminaChange(player,dt) local stamina = player:GetResource("MaxStamina") local correntStamina = player:GetResource("CorrentStamina") if sprintTable[player] then Task.Wait(0.1) correntStamina = correntStamina - 1 player.maxWalkSpeed = sprintingSpeed ..... end
According to my idea, the player has 100 units of endurance, and 1 unit is spent every 0.1 seconds. Therefore, 10 units should be spent in 1 second, and all 100 units of endurance should end in 10 seconds. BUT!!! When I checked the endurance consumption using a stopwatch, it turned out that 100 units of endurance are consumed in 15 seconds... Question: Why???
- I made a stimulator that temporarily increases health, and set it the following logic for the duration of the action time:
function Tick() for _, player in pairs(Game.GetPlayers()) do BigHPEnd(player) end end
function BigHPEnd (player) if player:GetResource("StimulantBigHP") == 1 then Task.Wait(1) local currentDurationBigHPTime = player:GetResource("CurrentDurationBigHPTime") currentDurationBigHPTime = currentDurationBigHPTime - 1 if currentDurationBigHPTime > 0 then player:SetResource("CurrentDurationBigHPTime", currentDurationBigHPTime) else .... end
Suppose, currentDurationBigHPTime = 10. I display the value of the currentDurationBigHPTime on the User Interface. I expect that every second the currentDurationBigHPTime will decrease by 1 (because Task.Wait(1)), but as a result I get the following sequence: 10, 8, 6, 4, 2. That is, the currentDurationBigHPTime is reduced not by 1 unit, but by 2. Question: why and what did I do wrong again? Thank you all in advance.