So I am trying to make a walking script, but when I press shift my walkspeed doesn’t get reduced. This should be a very simple script to make but I guess I’m that bad.
The script below is from a local script. No error message when I run the game
Playername = game.Parent.Parent
humanoid = game.Workspace.Playername.Humanoid
-- walking
userinputservice = game:GetService("UserInputService")
userinputservice.InputBegan:Connect(function(input,gameProccessedEvent)
if input.keycode == Enum.KeyCode.LeftShift then
humanoid.Walkspeed = 6
end
end)
userinputservice.InputBegan:Connect(function(input,gameProccessedEvent)
if input.keycode == Enum.KeyCode.LeftShift then
humanoid.WalkSpeed = 6
end
end)``` but its still not working. Maybe humanoid walkspeed cant be affected locally (doubt it cause exploiters can change WalkSpeed) but ill check anyway
Make sure to place it within StarterPlayerScript or another localscript friendly area!
local PlayerService = game:GetService('Players');
local UserInputService = game:GetService('UserInputService');
---------------------------------------------------------------------
UserInputService.InputBegan:Connect(function(keyPressed,currentlyOccupied)
if currentlyOccupied then return end
if keyPressed.UserInputType == Enum.UserInputType.Keyboard then
if keyPressed.KeyCode == Enum.KeyCode.LeftShift then
local localCharacter = PlayerService.LocalPlayer.Character
if not localCharacter then return end
local characterHumanoid = localCharacter:WaitForChild('Humanoid');
characterHumanoid.WalkSpeed = 20
end
end
end)
Thanks a lot. I don’t really understand this script much, which means I’ve got a lot to learn. Though, I guess today I learnt that localscripts can only run in 5 places. Again thanks a lot.
Firstly, you have the service variables, they just define the Roblox serives we’ll need to work. These variables are PlayerService and UserInputService. Then we have a line, this line is just for cosmetic purposes and does nothing but make the code look organized. After the line, we see a callback function for an event. A callback function is a function that is ran once something is complete or is called upon. The event we’re connecting the callback function to is the InputBegan event of the UserInputService. Once we make the function, we add two variables to it, keyPressed and, currentlyOccupied. The keyPressed variable is the input that was began. This can be a mouse button being pressed down, a key being pressed down, etc. The currentlyOccupied variable is the true/false(boolean) value if the player is currently doing something like typing. The first thing we do in the callback function is check if they’re typing, this is just a simple if statement. Once we know they’re not typing, we then do some checks to make sure that they key pressed is the one we want( Left Shift). Then from there we just find the character using PlayerService and the LocalPlayer value within it. Then the function checks if the character exists (Useful so we dont try to sprint when they die). If the character is there, we change the walkspeed! I hope this cleared up the code for you.