Maybe there’s something I’m missing. I’m trying to change this IntValue (PowerValue) that’s a child of my sprinting GUI, so I could use it for tracking how much stamina Power is left. I followed some similar code that works, but for some reason, it doesn’t work for this script. Help?
Edit: changed the chopped up script to the complete one just in case it helps. Problem is most likely the code, because I’ve changed the value to a NumberValue and its still not doing anything.
local UserInputService = game:GetService("UserInputService")
local Bar = script.Parent:WaitForChild('Background'):WaitForChild('Bar')
local StarterGui = game:GetService("StarterGui")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local NormalWalkSpeed = 12
local NewWalkSpeed = 19 -- sprinting speed
local Power = 10 -- how much stamina there is, don't mess with value of 10
local PowerRepresent = Power
local PowerRepresent = script.Parent.PowerValue.Value
local PowerIncrease = script.Parent.PowerAddValue.Value -- the amount of power being replenished when not running
if Player:FindFirstChild("IsSprinting") then -- finding or creating IsSprinting value
sprinting = Player.IsSprinting
print("Setup for IsSprinting value complete")
else
Instance.new("BoolValue", Player).Name = "IsSprinting"
Player.IsSprinting.Value = sprinting
print("Created new IsSprinting value")
end
local sprinting = false
local IsSprinting = Player.IsSprinting
local CrouchingSpeed = script.Parent.CrouchHandler.CrouchingSpeed.Value
local IsCrouching = Player:WaitForChild("IsCrouching") -- other Player Statuses setup
local IsHealing = Player:WaitForChild("IsHealing")
local IsAiming = Player:WaitForChild("IsAiming")
local IsReloading = Player:WaitForChild("IsReloading")
local IsAimFiring = Player:WaitForChild("IsAimFiring")
local IsHipFiring = Player:WaitForChild("IsHipFiring")
repeat wait() until game.Players.LocalPlayer.Character
local Anim = Instance.new('Animation')
Anim.AnimationId = 'rbxassetid://913376220' -- sprint animation ID
RunAnim = Character.Humanoid:LoadAnimation(Anim)
UserInputService.InputBegan:connect(function(key, gameProcessed)
if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
if IsCrouching.Value == true or IsHealing.Value == true or IsAiming.Value == true or IsReloading.Value == true
or IsAimFiring.Value == true or IsHipFiring.Value == true then
return
end
if Character.Humanoid.MoveDirection.Magnitude > 0 then
RunAnim:Play()
Character.Humanoid.WalkSpeed = NewWalkSpeed
sprinting = true
IsSprinting.Value = true
while Power > 0 and sprinting do
Power = Power - 0.03 -- the amount of stamina decreased when running
UpdatePower(PowerRepresent)
Bar:TweenSize(UDim2.new(Power / 10, -1, 0.6, 4), 'Out', 'Quint', .05, true)
wait()
if Power <= 0 then -- less than or equal to 0
IsSprinting.Value = false
Character:WaitForChild("SoundPart").HeavyBreathing:Play()
RunAnim:Stop()
Bar.BackgroundColor3 = Color3.fromRGB(203, 0, 0) -- changes color to red
Character.Humanoid.WalkSpeed = NormalWalkSpeed
end
end
end
end
end)
UserInputService.InputEnded:connect(function(key, gameProcessed)
if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
if IsCrouching.Value == true then
return
end
if Character.Humanoid.MoveDirection.Magnitude > 0 then
RunAnim:Stop()
Character.Humanoid.WalkSpeed = NormalWalkSpeed
sprinting = false
IsSprinting.Value = false
while Power < 10 and not sprinting do
RunAnim:Stop()
Power = Power + PowerIncrease
UpdatePower(PowerRepresent)
Bar:TweenSize(UDim2.new(Power / 10, -1, 0.6, 4), 'Out', 'Quint', .05, true)
wait()
if Power <= 3 then -- less than or equal to 3
RunAnim:Stop()
sprinting = false
IsSprinting.Value = false
if IsCrouching.Value == true then
Character.Humanoid.WalkSpeed = CrouchingSpeed
else
Character.Humanoid.WalkSpeed = NormalWalkSpeed
end
end
end
end
end
end)
local RunService = game:GetService("RunService")
RunService.RenderStepped:Connect(function()
if Power >= 3 then -- greater than or equal to 3
Bar.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
Character:WaitForChild("SoundPart").HeavyBreathing:Stop()
end
end)
RunService.RenderStepped:Connect(function()
if Power <= 3 then -- less than or equal to 3
Bar.BackgroundColor3 = Color3.fromRGB(203, 0, 0)
end
end)
function UpdatePower(value)
script.Parent.PowerValue.Value = script.Parent.PowerValue.Value
script.Parent.PowerValue.Value = PowerRepresent
end