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)
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
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)