I'm trying to create a script whereby when the key "e" is pressed the player's speed is adjusted to 200 and when the key "e" is released the speed is adjusted back to normal.
Player = game.Players.LocalPlayer if Mouse.KeyDown:connect(function(key) if key == "e" then game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 200 end end)
Do not use if ..:Connect
only use ...:Connect
, :connect and Mouse.KeyDown/Up
is deprecated. use :Connect and UserInputService.InputBegan/InputEnded
.
Example:
--< LocalScript local uis = game:GetService("UserInputService") -- service to detect key's local plr = game.Players.LocalPlayer -- get local player local char = plr.Character or plr.CharacterAdded:Wait() -- wait for character added / get char uis.InputBegan:Connect(function(input,proc) if not proc and input.KeyCode == Enum.KeyCode.E then -- On press E char:WaitForChild("Humanoid").WalkSpeed = 200 -- Wait for humanoid and set speed end end) -- You want to reset speed on stop walking? use uis.InputEnded --< You can remove this uis.InputEnded:Connect(function(input,proc) if not proc and input.KeyCode == Enum.KeyCode.E then -- Detect if key Up char:WaitForChild("Humanoid").WalkSpeed = 16 end end)
What is UserInputService
?
What is UserInputService.InputBegan
?
What is UserInputService.InputEnded
?
What is proc
?
ContextActionService
This is in wiki.
Hope it helped :)
Wiki pages: