I’m creating an energy bar and im trying to tween it so that when the player does an action that causes the energy variable to drop, the bar will deplete/lower. But the issue is that when I press M1 which is what the move that tweens the bar is set to, nothing happens until I move again (which updates the energy variable that the bar is based on).
script
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")
local energy = 0
local energyBar = script.Parent
local running = false
energyBar:TweenSize(UDim2.new(1,0,energy/25,0),"Out", "Linear", 0)
humanoid.Running:Connect(function(speed)
running = speed > 5
end)
task.spawn(function()
while true do
if running then
if energy < 25 then
energy += 1
energyBar:TweenSize(UDim2.new(1,0,energy/25,0),"Out", "Linear", 0.1)
end
end
task.wait(.1)
end
end)
UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 and energy >= 15 then
energyBar:TweenSize(UDim2.new(1,0,energy/25,0),"Out", "Linear", 0.5)
energy = 0
end
end)
video