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

Why won't this KeyDown Function that's supposed to add speed work?

Asked by 7 years ago
Edited 7 years ago

I made this small script and placed it in StarterPack but it's not working. It's supposed to add 50 speed everytime I press "q".

player = game.Players.LocalPlayer
mouse = player:GetMouse()
c = player.Character
h = c.Humanoid
ws = h.WalkSpeed

mouse.KeyDown:Connect(function(key)
    if (key == "q") then
        ws = ws + 50
    end
end)
0
Not answering to explain but a quick fix is: Delete your "ws = h.WalkSpeed" variable (Use Locals my man) and change the "ws" part inside the function to "h.WalkSpeed = h.WalkSpeed + 50". There's so many better ways to do this and you could probably start with UserInputService instead of using KeyDown from the mouse. Someone nice out here might be able to explain it too you if they can. xPolarium 1388 — 7y
0
ok thanks Inpolite 44 — 7y
0
but why does that make a difference Inpolite 44 — 7y

1 answer

Log in to vote
2
Answered by 7 years ago
Edited 7 years ago

I have not tested it out, it might not work. An important thing is that you should put local before each of your variables. An another important thing that you should use is UserInputService or ContextActionService, both of those services do a lot(ContextActionService can create custom buttons on the screen and with UserInputService, you could check what device the player is using and much more stuff)

local player = game.Players.LocalPlayer
local uis = game:GetService("UserInputService")
local char = player.Character
local hum = char.Humanoid
local amount = 50
uis.InputBegan:Connect(function(key, gpe) 
    if key.KeyCode == Enum.KeyCode.Q and not gpe then -- gpe stands for GameProcessedEvent
        hum.WalkSpeed = hum.WalkSpeed + amount
    end
end)
0
You should wait for the character to load, and use WaitForChild for the humanoid. CootKitty 311 — 7y
Ad

Answer this question