Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to fix while loop using humanoind.Running speed not breaking properly?

Asked by 3 years ago
Edited 3 years ago

I'm trying to make it so that the print will count up by 1 point/bValue per second only while walking, but my script's while loop never breaks for some reason, so it works for the most part, it just doesn't stop counting when the player stops walking.

-- local bValue = script.Parent.Parent.Parent.StarterGui.ScreenGui.TextButton.bValue (code for later)
local bValue = 0
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

humanoid.Running:Connect(function(speed)
    while speed >= 16 do
        wait(1)
        -- bValue.Value = bValue.Value + 1 (also for later)
        bValue = bValue + 1
        --print(bValue.Value)
        print("bValue is: " .. bValue)
        print("Speed is: " .. speed)
        if speed < 16 then
            break
        end
    end
end)

2 answers

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

Speed changes constantly which fires many Running event, but the speed variable never changes in each fired thread, so you basically just spammed while loops in the process, which causes neverending counting.

You should only add an if condition to the event that only works when your speed is equal or above 16.

Added debounce to suit your needs - this'll stop the event from being executed until the debounce is disabled.

local debounce
humanoid.Running:Connect(function(speed)
    if speed >= 16 and not debounce then
        debounce = true
        --Do stuff heree
        wait(1) --Delay execution for a second
        debounce = false
    end
end)

EDIT: I guess you'll have to use a while loop after all... Here's something you can work with.

local spd = 0 --We shall update our speed here
local delay = 1 --Delay things by a second, there's a substitute I wanna share instead of using wait(1)

local debounce = 0 --Debounce can be anything!

local bValue = 0 --The one you wanna give but doesn't care how much you're trying to give.

--Use Heartbeat for a bit of precision
local runService = game:GetService('RunService')

--Here is everything you gotta do
humanoid.Running:Connect(function(speed)
    spd = speed --Updates to current speed
end)

--Now, use Heartbeat to increase the value
--I forgot your value name so I'll just call it bValue
--Edit: nvm it's correct
--This works pretty much the same way as while loops... but fires every after a frame of physics simulation is completed.
--Debounce here acts as a cooldown tbh
runService.Heartbeat:Connect(function(dt)
    --dt is delta time, it means the number of seconds that has elapsed since the last finished frame.
    --Now let's check if debounce is available.
    if debounce > 0 then
        --This must've meant that the script is delayed. Decrease debounce until it reaches zero, so during the next frame's completion it'll be run again.
        debounce = debounce - dt --in luau use debounce -= dt
        --Don't worry about debounce being under 0! We don't care about it if it is, technically.
    elseif spd >= 16 then --If debounce > 0 and the speed recorded exceeds 16
        debounce = delay --This basically tells the debounce to delay the process like wait()
        --Now add something to ur value
        bValue = bValue + 1
        --Print bValue and speed
        print(string.format('added bvalue by one, final result: %d\nSpeed is %d', bValue, spd))
        --you can put debounce = delay here in case the operation above may cause errors and you dont want it to be delayed if it is faulty
    end
end)

Also, the speed output may not be consistent - but if there's a reason for you to use it, I would say sure, don't hold your horses :d

0
So I implemented debounce, and removed the while loop, the code now runs once when I start walking, but doesn't add 1 point/sec while walking, and after 3 points added the script broke all together, also the wait has to go first as to stop people from spamming w to get points quicker SpelunkyGaming 34 — 3y
0
Update: fixed the script breaking issue, but now the 2nd if statement that's meant to check if the player is still walking after 1 second doesn't work as intended, and the 1point/sec still doesn't work while continuously walking. SpelunkyGaming 34 — 3y
0
@SpelunkyGaming Oh, I forgot! When the player walks, the speed will mostly be maintained. I'll put a new fix in the answer. Afterl1ght 321 — 3y
0
ok! thank you, btw! I'm pretty new to scripting and things like this are a huge help. SpelunkyGaming 34 — 3y
View all comments (8 more)
0
@SpelunkyGaming (is tagging this way works) I edited my post! Hope that works well on both you and your game, I'm currently on mobile rn haha so I'm sorry it may go out faulty since it's untested. Gimme a knock if it does or does nuh! Afterl1ght 321 — 3y
0
@SpelunkyGaming edited again... some minor fixes. with explanations Afterl1ght 321 — 3y
0
I'm afraid pinging doesn't do much (It'd be cool if it did though), however I'm working with the comments and resources you gave me, and I'm testing a bunch f things now, this is extremely helpful! Especially since I've heard terms like DeltaTime before but didn't quite know what it was. SpelunkyGaming 34 — 3y
0
Simply speaking, delta time is like time difference: timePresent - timePast = deltaTime. Delta generally means difference in variables in mathematics as I've learnt, etc. delta also stands for the triangle letter thing uknow greek stuff Afterl1ght 321 — 3y
0
Anyways I wish you goodluck on whatever you're doing rn! Afterl1ght 321 — 3y
0
Again, thank you so much for helping me get this fixed, and thanks for the good luck, I'm sure I'll need it lol. Other than that the TextLabel GUI is being annoying but It's probably a simple fix... *probably* SpelunkyGaming 34 — 3y
0
No problem! Hmm, are you trying to get the gui in StarterGui instead of PlayerGui? StarterGui's children will be parented to the player's PlayerGui upon joining, which you'll have to use Player.PlayerGui for the result to be visible Afterl1ght 321 — 3y
0
well the script correctly sets the text in the properties tab, but it doesn't change the text in the actual game preview SpelunkyGaming 34 — 3y
Ad
Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

The "final" (The actual script I'll be using will be a modified version of this that'll work better with me and my friends' team create game) script for anyone curious, HUUUUGE thanks to Afterl1ght for not only helping me fix my script but for teaching me a few things as well!

local AP = script.Parent.Parent.Parent.StarterGui.ScreenGui.Frame.APLabel.APValue
local APText = script.Parent.Parent.Parent.StarterGui.ScreenGui.Frame.APLabel
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local spd = 0
local delay = 1
local debounce = 0

local runService = game:GetService("RunService")

humanoid.Running:Connect(function(speed)
    spd = speed
end)

runService.Heartbeat:Connect(function(dt)
    if debounce > 0 and spd >= 16 then
        debounce = debounce - dt
    elseif spd >= 16 then
        debounce = delay
        AP.Value = AP.Value + 1
        APText.Text = AP.Value
        print("You have: " .. AP.Value .. " Adventure Points.")
    end
end)

Answer this question