Attempt to call a Udim2 value

Viewed 26

I am making a stamina bar for my game but I keep getting an Attempt to call Udim2 value error whenever I press LeftShift, I get the error. What is happening, could anyone please help? Here is the code:

    local player = game.Players.LocalPlayer
    local charcter = player.Character

    local UserInputService = game:GetService("UserInputService")

    local Stamina = 100
    local running = false

    Stamina = math.clamp(Stamina, 0, 100)

    UserInputService.InputBegan:Connect(function(input)
        if input.KeyCode == Enum.KeyCode.LeftShift then
            running = true
            charcter.Humanoid.WalkSpeed = 22
            while Stamina > 0 and running do
                Stamina = Stamina-0.6
                
                script.Parent:TweenSize(UDim2.new(Stamina/ 100, 0, 1, 0) "Out", "Linear", 0)
                
                wait()
                if Stamina == 0 then
                    charcter.Humainoid.WalkSpeed = 16
                    
                end
            end
        end
    end)

    UserInputService.InputEnded:Connect(function(input)
        if input.KeyCode == Enum.KeyCode.LeftShift then
            running = false
            charcter.Humanoid.WalkSpeed = 16
            while Stamina < 100 and not running do
                Stamina = Stamina+0.5

                script.Parent:TweenSize(UDim2.new(Stamina/ 100, 0, 1, 0) "Out", "Linear", 0)
                 wait()
                if Stamina == 0 then
                    charcter.Humainoid.WalkSpeed = 16

                end
            end
        end
    end)
1 Answers

Your issue is caused by a typo. You've forgotten to put a comma after each of your UDim2.new function calls :

UDim2.new(Stamina/ 100, 0, 1, 0) "Out",

The reason this is giving you an Attempt to call a UDim2 value error, is because of the lua language will allow you to call a function without parentheses when there is only one argument provided. For example, print "hello world" works exactly the same as print("hello world"). So because you've forgotten to put the comma after the UDim2 creation, the interpreter assumes that you're trying to call it like a function.

As for making a stamina bar, try something like this :

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local player = game.Players.LocalPlayer

local stamina = 100
local maxStamina = 100
local running = false
local sprintCost = -0.6 -- per second
local sprintRecovery = 0.5 -- per second
local speedWalk = 16
local speedRun = 22

-- whenever the game ticks, update stamina
RunService.Heartbeat:Connect(function(deltaTimeS)
    -- quick escape if the player's character isn't loaded
    if not (player.Character and player.Character.Parent ~= nil) then
        return
    end

    -- quick escape if nothing is happening and nothing needs to be updated
    if stamina == maxStamina and (not running) then
        return
    end

    -- update stamina
    local change = if running then sprintCost else sprintRecovery
    stamina = math.clamp(stamina + (change * deltaTimeS), 0, maxStamina)

    -- set the run speed
    local isRunning = stamina > 0 and running
    player.Character.Humanoid.WalkSpeed = if isRunning then speedRun else speedWalk

    -- also keep the UI updated
    script.Parent.Size = UDim2.new(stamina / maxStamina, 0, 1, 0)
end)

-- when the user holds left shift, attempt to start running
UserInputService.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        running = true
    end
end)

-- when the user lets go of left shift, stop running
UserInputService.InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        running = false
    end
end)

I moved your loop logic outside of the InputBegan and InputEnded handlers, and instead put it inside a RunService.Heartbeat connection. This decouples the running logic from state changes, and it keeps your UI updated with every frame tick without needing to use Tweens. I also defined all of your magic numbers as variables near the top of the script, this will make it much easier to tweak values as you playtest.

Related