Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Hold left shift to sprint, release left shift to revert to old stats?

Asked by 6 years ago
DirectoryKeys["LeftShift"] = function()
local Dude = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")

    if Sprinting == false then
    local PriorWalkSpeed = Dude.WalkSpeed
    local PriorWR = WR.Value
    local PriorStance = Stance.Value
    Sprinting = true
    Dude.WalkSpeed = 22
    Stance.Value = 3

        if PrimAnim == true then
        WR.Value = 2
        else
        WR.Value = 1
        end

    WeaponReadiness()
    StancePosition()
    script.Parent.Parent.PlayerGui.WeaponUI.Frame.SPSDisplay.Text = "WalkSpeed: "..Dude.WalkSpeed
    else
    Sprinting = false
    WR.Value = 3
    Stance.Value = 3 -- These three numbers here are where the "memorized stances" go.
    Dude.WalkSpeed = 13.5
    WeaponReadiness()
    StancePosition()
    script.Parent.Parent.PlayerGui.WeaponUI.Frame.SPSDisplay.Text = "WalkSpeed: "..Dude.WalkSpeed
    end

end

If I wanted to sprint only when holding the Left Shift button down, saving the WalkSpeed, WeaponReadiness level, and Stance level in between calling the function and beginning the sprinting, and then stop sprinting when I release the Left Shift button and then revert to the previous stats using the saved variables, how would I go about this?

Any attempts I've made either result in the stats not saving properly and any buttons that aren't shift (while left shift is held down) firing the function and breaking the sprint or re-activating it, or for some reason not being able to pull out my primary or secondary weapons and even bugging when trying to pull the second weapon out pressing "2", going as far as to continuously run in a single direction until right clicking...?

Any help would be greatly appreciated.

[Note: This code is what I began with up here. The code below is what I have currently.]

Mouse.KeyDown:connect(function(Key)  -- Works; but the local Priors need to be redefined to whatever the stats are between the time the Left Shift
local Dude = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")     -- is pressed and the time that the sprinting actually begins.
local PriorWalkSpeed = Dude.WalkSpeed                                                               -- code works otherwise.
local PriorWR = WR.Value
local PriorStance = Stance.Value                                -- For some reason the ability to draw primary and secondary have broken while sprinting.
                                                                                -- Yay, more problems to fix! ... Just what I wanted... - _ -
    if Key:byte() == 48 then

        if Sprinting == false then
        Sprinting = true
        Dude.WalkSpeed = 22
        Stance.Value = 3

            if PrimAnim == true then
            WR.Value = 2
            else
            WR.Value = 1
            end

        WeaponReadiness()
        StancePosition()
        script.Parent.Parent.PlayerGui.WeaponUI.Frame.SPSDisplay.Text = "WalkSpeed: "..Dude.WalkSpeed

            Mouse.KeyUp:connect(function(Key)   

                if Key:byte() == 48 then
                Sprinting = false
                WR.Value = PriorWR
                Stance.Value = PriorStance
                Dude.WalkSpeed = PriorWalkSpeed
                WeaponReadiness()
                StancePosition()
                script.Parent.Parent.PlayerGui.WeaponUI.Frame.SPSDisplay.Text = "WalkSpeed: "..Dude.WalkSpeed   
                else
                end

            end)

        end

    end

end)

Again, any assistance would be great.

To summarize: Holding Left Shift should swiftly save or overwrite the save of your current WalkSpeed, Stance and WR levels before going into the actual sprinting function. ONLY Left Shift should be handling Sprinting independently of everything else.

When Left Shift is released, it should end the Sprinting function by reverting to the previously saved or overwritten WalkSpeed, Stance and WR level to create a seamless sprinting system.

0
Try defining at the start of the script a variable called oldspeed, and when you press a key, even before it sets the speed, it sets the oldspeed variable to the user's speed. Then, set the user's speed to the oldspeed variable. brokenVectors 525 — 6y

1 answer

Log in to vote
-1
Answered by 6 years ago
Edited 6 years ago

Ok so heres the code.When you press shift the walkspeed turns to 50 aka your running and when your not pressing shift your speed goes to 20 aka walking.Im a new scripter so it might not work.But this is the script.

local zeb=game:GetService("UserInputService")
zeb.InputBegan:connect(function(key,processed)
if no proc and Input.Keycode=enum.Keycode.Shift then
game.Players.LocalPlayer.Humanoid.WalkSpeed=50--change if you want
end
zeb.InputEnded:connect(function()
game.Players.LocalPlayer.Humanoid.Walkspeed=25--the devault walkspeed
end)
end)

Also tell me if this works and tell me how good of a scripter i am :D.If it dont work tell me what i need to fix

0
So heres the script if it dosent work tell me what i need to fix. User#20192 0 — 6y
1
Well firstly, the Humanoid isn't found in the LocalPlayer, secondly "no proc" is going to throw an error (I'm not sure why, but it probably has to do with syntax and variables), thirdly I don't think Keycode = enum.KeyCode.Shift will work because "Enum.KeyCode.LeftShift" should be in parenthesis. Oh, also your first "end" shouldn't have ")" at the end of it. Char187 0 — 6y
0
no proc wont make an error no proc means when there not typing in the roblox chat. If no proc dont work then say processed,or just delete it.And humanoid is found in local player because thats how you define walkspeed.And lastly ")" should be at the end of the end because when you end a event (or function) you put a "end)" at the end of it. soooo. Get your facts straight User#20192 0 — 6y
1
Mnkayyy... I mean... it didn't work for me and it had like 4 errors in it but... aalllrrriiiiiggght. Char187 0 — 6y
View all comments (7 more)
0
Also "no" isn't a Lua keyword..... T0XN 276 — 6y
0
I believe you meant 'not'. The Humanoid is not located within the LocalPlayer either. T0XN 276 — 6y
0
local humanoid = game.Players.LocalPlayer.Character.Humanoid T0XN 276 — 6y
0
In addition, "enum" should be "Enum" with a capital. I'm sure everyone appreciates your effort, but I don't think you're at the right stage to be answering questions. T0XN 276 — 6y
0
On top of that "Shift" isn't a key. It should either be "LeftShift" or "RightShift". I could keep going if you want.... :/ T0XN 276 — 6y
0
":connect" is deprecated, so that should be ":Connect" instead. T0XN 276 — 6y
0
Also you need to end the "zeb.InputBegan" function before the "zeb.InputEnded" function begins. T0XN 276 — 6y
Ad

Answer this question