Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

12 comments

0

Gah... ROBLOX Studio is so stupid and unindented my script. Bare with me please :$

0

Put your code in a code block -- that little blue Lua symbol. Also, don't just post code. Explain your problem in detail.

1

I did explain what I needed to fix in the question...

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.

1

yeah post explanation pls

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?

0

I'm not saying this is a great question, but it's not bad either. It has enough explanation.

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.

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.

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.

0

can't you just read the script and question then answer from there? lol? why downvote a question.

2 answers

✓ Accepted answer

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 comments

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.

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.

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

1 comment

1

No, it doesn't. 'keyu' is an identifier for which key is being Pushed Up, or, being let go.

This discussion is preserved as it was. New answers, comments, and votes are closed.