How would I go about making a function that only runs when a value is at a certain state? For example, say I want to make an energy system. I want the energy to only regen when it is not sprinting. I tried using a while loop, but it didn’t work.
It very much depends on what you’re making, for the sprinting system you mentioned, you can use something like this:
local sprinting = false
local energy = 100
local ENERGY_DECREASE_RATE_WHILE_SPRINTING = 3 -- per second
local function turnSprintOn()
sprinting = true
task.defer(function()
while sprinting do
local dt = task.wait()
energy -= ENERGY_DECREASE_RATE_WHILE_SPRINTING * dt
end
end)
end
local function turnSprintOff()
sprinting = false
end
It’s basically event driven code, that when you start sprinting, it’ll keep ticking down the energy until you stop sprinting
something like this, but please, learn basic scripting, this isn’t too hard:
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")
local RunningSpeed = 30
local CurrentStamina = 100
local IsRunning = false
local function UpdateStamina()
while true do
task.wait(0.1)
if IsRunning and CurrentStamina > 0 then
CurrentStamina = CurrentStamina - 1
elseif not IsRunning and CurrentStamina < 100 then
CurrentStamina = CurrentStamina + 1
end
if CurrentStamina <= 0 then
IsRunning = false
Humanoid.WalkSpeed = 16
end
end
end
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift and CurrentStamina > 0 then
Humanoid.WalkSpeed = RunningSpeed
IsRunning = true
end
end)
UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
Humanoid.WalkSpeed = 16
IsRunning = false
end
end)
task.spawn(UpdateStamina)
For example, have a bool value set as an instance inside the same script doing the energy/sprinting. After that, just update the value each time the player is sprinting. Apply the :GetPropertyChangedSignal
and check if its true or false. If sprinting is false, do the regen loop with an external bindable connection saying that it will break on .Event
.
Here is an example:
local Bindable = Instance.new("BindableEvent")
Value:GetPropertyChangedSignal("Value"):Connect(function()
if Value.Value == false then
while true do
task.wait(regen_time)
--Do regen stuff here:
Bindable.Event:Connect(function()
break
end)
end
else
Bindable:Fire()
end
end)
idk if this would work. Might contain some syntax errors or some stupid stuff I forgot to check But try it out!
(I might be overcomplicating this, but it gave a basic idea of the script structure you need)
I don’t exactly get the necessity of a BindableEvent
if you aren’t using it between scripts, or the necessity of a BoolValue
over just a variable
This code will also leak memory a lot
I was able to get all the hard stuff like the actual running script (with tweens for fov and everything!) and yet I can’t do this lol.
Break down everything into smaller chunks, or write pseudo code, solving a problem is combining small chunks to complete a bigger one, like a puzzle basically.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.