#Grabbing a local variable inside of a function without actually making a global variable

2 messages · Page 1 of 1 (latest)

vast cargo
#

Keybind function (its super scuffed right now since i've been trying like 20 different methods)

local function Keybind(Player, Key, Holding)
    local HoldingKey = Holding -- This is just for making sure that the player is holding the key for moving back and forth. no other keybinds will use Holding.
    local UID = Player.UserId -- we HAVE to use the players UID each time or else the Main modulescript wouldnt know whos trying to grab a stat.
    local CurrentState = MainFunction.GetStat(UID, "Stance") -- This grabs our state every time we hit a keybind.
    print("Recieved:" , Key, Holding, CurrentState)
    
    if HoldingKey == false then
        if CurrentState == 4 then
            P1WalkBackwardTrack:Stop()
            MainFunction.UpdateStat(UID, "Stance", 0)
            P1StandingIdleTrack:Play()
        elseif CurrentState == 3 then
            P1WalkForwardTrack:Stop()
            MainFunction.UpdateStat(UID, "Stance", 0)
            P1StandingIdleTrack:Play()
        elseif CurrentState == 5 then
            MainFunction.UpdateStat(UID, "Stance", 0)
            P1StandingIdleTrack:Play()
        end
        
    end
    
    if HoldingKey == true then
        if Key == Enum.KeyCode.A then -- WalkBackward, State 4
            if CurrentState == 0 then -- Are we standing?
                MainFunction.UpdateStat(UID, "Stance", 4) -- This updates our stat immediately so that no other moves can be made.
                P1WalkBackwardTrack:Play() -- Then finally plays that animation.
            end
        end
        if Key == Enum.KeyCode.D then -- WalkForward, State 3
            if CurrentState == 0 then
                MainFunction.UpdateStat(UID, "Stance", 3)
                P1WalkForwardTrack:Play()
            end
        end    
    end
    
    if Key == Enum.KeyCode.C then -- Crouch, State 1
        if CurrentState == 0 then -- Standing?
            MainFunction.UpdateStat(UID, "Stance", 1) -- Were crouching now
            P1CrouchingTrack:Play() -- Play Crouch Anim
            -- P1CrouchingIdleTrack:Play()
        elseif CurrentState == 2 then -- Crouch to standing
            P1CrouchingIdleTrack:Stop() -- SHTAHP
            MainFunction.UpdateStat(UID, "Stance", 0) -- Were standing now
            P1StandingIdleTrack:Play() -- Back to default
        end
    end    
    
    if Key == Enum.KeyCode.E then -- INDEV DashForward, State 5
        if CurrentState == 0 then
            MainFunction.UpdateStat(UID, "Stance", 5)
            P1DashStartTrack:Play()
        end
    end
    if Key == Enum.KeyCode.F then -- INDEV dive, State 7
        if CurrentState == 5 then
            MainFunction.UpdateStat(UID, "Stance", 7)
            P1DashtoDiveTrack:Play()
        end
    end
    
    HoldingKey = nil -- set everything to nil to ensure that we can use the function again immediately.
    UID = nil
    CurrentState = nil
end
#

The events in question. the commented out MainFunction.UpdateStat is what i need the variable in. (UID is nil in this case unless i put it in the function, but then it runs multiple times!!!)

P1DashStartTrack.Stopped:Connect(function() -- DashForward Ending...
    print("Dash end test")
    P1DashEndTrack:Play()
end)
P1DashEndTrack.Stopped:Connect(function() -- Back to Standing, State 0
    
    -- MainFunction.UpdateStat(UID, "Stance", 0)
    print("Dun! :)")
end)