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

KeyDown function help?[Solved]

I'm trying to make a script that makes you walk faster every time you press "w" but I can't get it to work.

local Player = game.Players.LocalPlayer

game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(key)
    if string.lower(key) ==  "w" then
    Human = game.Workspace:FindFirstChild(Player)
    Human.Humanoid.Walkspeed = Human.Humanoid.Walkspeed + 5
    end
end)

ANY help is appreciated :D

1 comment

0

is there way to find if the character is moving or not?

3 answers

✓ Accepted answer

**Your problem is that you didn't capitolize the 's' in 'Walkspeed'. It should be 'WalkSpeed'.**

You're also doing some things inefficiently.

1) You should make a variable for the mouse, then use the event off of that variable. It makes the readability of your code much better.

2) You should access the Humanoid through the Player's Character.

3) You shouldn't add the WalkSpeed, because then if they spam the 'w' button then they can get like 500 WalkSpeed. Instead, set the WalkSpeed to the original(16) plus five, so 21.

Also, you should make a KeyUp event, with the letter, to reset the walkspeed back to normal so they don't run forever after they press the button once.

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse
local extraSpeed = 21
local regSpeed = 16

mouse.KeyDown:connect(function(key)
	if key:lower() == "w" and plr.Character then
		local hum = plr.Character:FindFirstChild('Humanoid')
		if hum then
			hum.WalkSpeed = extraSpeed
		end
	end
end)

mouse.KeyUp:connect(function(key)
	if key:lower() == "w" and plr.Character then
		local hum = plr.Character:FindFirstChild('Humanoid')
		if hum then
			hum.WalkSpeed = regSpeed
		end
	end
end)

1 comment

0

Yeah I figured that out, I saw that in output, and the point to it is to gain walkspeed everytime you press the button

For what you are doing, the neatest way to go about this is to perhaps use ContextActionService:

plr = game.Players.LocalPlayer or game.Players.LocalPlayer:wait()
chr = plr.Character or plr.Character:wait()
context = game:GetService("ContextActionService")

context:BindActionToInputTypes(
"walkspeedIncrease",
function() chr.Humanoid.Walkspeed = chr.Humanoid.WalkSpeed + 5 end,
true,
Enum.KeyCode.W
)

The line ** Human = game.Workspace:FindFirstChild(Player)** is wrong. You should replace that with:

Player.Character

2 comments

0

Thanks

0

Although this is accurate it isn't what was causing the problem. Yes, BSIncorperated should get the Humanoid this way but his way works too. The Real problem was that he wasn't capitolizing 'WalkSpeed'. This answer is invalid!!

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