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

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

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)

4 comments

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.

0

ok thanks

0

but why does that make a difference

1 answer

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)

1 comment

0

You should wait for the character to load, and use WaitForChild for the humanoid.

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