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".

01player = game.Players.LocalPlayer
02mouse = player:GetMouse()
03c = player.Character
04h = c.Humanoid
05ws = h.WalkSpeed
06 
07mouse.KeyDown:Connect(function(key)
08    if (key == "q") then
09        ws = ws + 50
10    end
11end)
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)

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

Answer this question