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

My script doesn't stop sprinting after you stop pressing the shift key? Help?

Asked by 9 years ago
local p = game.Players.LocalPlayer
local m = p:GetMouse()
local ani = Instance.new("Animation",p.Character or p.CharacterAdded:wait())
ani.AnimationId = "http://www.roblox.com/asset?ID=214048516"
local play = false

m.KeyDown:connect(function(key)
    if key:lower() == "w" or "s" or "a" or "d" then
        if key:byte() == 48 then
            play = true
            if play then
                anim = p.Character:FindFirstChild("Humanoid"):LoadAnimation(ani)
                p.Character.Animate.Disabled = true
                anim:Play()
                p.Character.Humanoid.WalkSpeed = 21
            end
        end
    end
end)

m.KeyUp:connect(function(keyu)
    if keyu:lower() == "a" or "w" or "s" or "d" then
        return
    elseif keyu:byte() == 48 then
        print("sprint disabled")
        p.Character.Animate.Disabled = false
        anim:Stop()
        p.Character.Humanoid.WalkSpeed = 16
        play = false
    end
end)
0
Gah... ROBLOX Studio is so stupid and unindented my script. Bare with me please :$ 0xDEADC0DE 310 — 9y
0
Put your code in a code block -- that little blue Lua symbol. Also, don't just post code. Explain your problem in detail. Perci1 4988 — 9y
1
I did explain what I needed to fix in the question... 0xDEADC0DE 310 — 9y
0
@Perci; learn to read. The title here gives all the detail we need. @OP; I'll try to figure out why your code isn't working. For now, I'll answer with a more preferrable method for doing sprinting or other character-mods via a held-down key. adark 5487 — 9y
View all comments (8 more)
1
Adark, I suggest you learn to read. https://scriptinghelpers.org/help/how-post-good-questions-answers Perci1 4988 — 9y
1
yeah post explanation pls ZeptixBlade 215 — 9y
0
@Perci; "My script doesn't stop sprinting after you stop pressing the shift key? " What more explanation do you need? A line-by-line commentation of the code? adark 5487 — 9y
0
I'm not saying this is a *great* question, but it's not bad either. It has enough explanation. adark 5487 — 9y
0
I'm trying to follow the rules. I don't know EXACTLY what else would be put, because I'm not the person testing it, but more info should be given. Perci1 4988 — 9y
0
Nothing else *needs* be put is the entire point. Requiring text besides code in a Question is a guideline, not a requirement. Also, *you* are breaking a rule of commenting on something you have nothing to contribute to. If you can't see any way to add more explanation to this question, you obviously don't have a real reason to say that one's needed, besides the guideline that it should be there. adark 5487 — 9y
0
I'm getting tired of your attitude. More information is never NEEDED, but it helps people answer, which is the point. Some possible things to add would be hierarchy, output, type of script, if the WalkSpeed is changing, if the animation is playing, the goal, previous attempts at debugging, etc. Perci1 4988 — 9y
0
can't you just read the script and question then answer from there? lol? why downvote a question. 0xDEADC0DE 310 — 9y

2 answers

Log in to vote
2
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

To explain the method, you should try and handle all the actual character modding stuff in the KeyDown bit, using either a while loop or a variable-change catch repeat loop like I use here, and only cancel the loop in the KeyUp. It's a bit safer and has the mod-on and mod-off stuff in one place for each mod:

local p = game.Players.LocalPlayer
local m = p:GetMouse()
local ani = Instance.new("Animation",p.Character or p.CharacterAdded:wait())
ani.AnimationId = "http://www.roblox.com/asset?ID=214048516"
local play = false

m.KeyDown:connect(function(key)
    if key:lower() == "w" or key:lower() == "s" or key:lower() == "a" or key:lower() == "d" then
    elseif key:byte() == 48 then
        play = true
        anim = p.Character:FindFirstChild("Humanoid"):LoadAnimation(ani)
        p.Character.Animate.Disabled = true
        anim:Play()
        p.Character.Humanoid.WalkSpeed = 21
        repeat wait() until not play --Waits for the KeyUp
        anim:Stop()
        p.Character.Animate.Disabled = false
        p.Character.Humanoid.WalkSpeed = 16
    end
end)

m.KeyUp:connect(function(keyu)
    if keyu:lower() == "a" or key:lower() == "w" or key:lower() == "s" or key:lower() == "d" then
        return
    elseif keyu:byte() == 48 then
        print("sprint disabled")
        play = false
    end
end)

2
There are errors on lines 8 and 23; You are saying 'if key is equal to w or string s or string a or string d', it is not checking if s,a, or d are a key, just being used as normal strings in the 'if' statement. :P Those lines may be one of the main problems in the code. TheeDeathCaster 2368 — 9y
0
Didn't even notice that. I wrap this to a Table for easier use so I don't write this specific type of code often. adark 5487 — 9y
Ad
Log in to vote
-2
Answered by 9 years ago

Line 22 u wrote keyu isnt it key? ;o think it matters

1
No, it doesn't. 'keyu' is an identifier for which key is being Pushed Up, or, being let go. TheeDeathCaster 2368 — 9y

Answer this question