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

KeyDown function help?[Solved]

Asked by 9 years ago

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

0
is there way to find if the character is moving or not? BSIncorporated 640 — 9y

3 answers

Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

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)
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 BSIncorporated 640 — 9y
Ad
Log in to vote
4
Answered by 9 years ago

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
)

Log in to vote
1
Answered by 9 years ago

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

Player.Character
0
Thanks BSIncorporated 640 — 9y
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!! Goulstem 8144 — 9y

Answer this question