##The Problem
You're trying to index the player's mouse as well use client inputs from the server. You can't do this. The server is different from the client.
##Note
This is only going to include how to make something like a sprint system (assumed infinite stamina.)
It is 5:43 PM (ok its not that late but I am tired still). and I am being a bit lazy. Decided to not include how to set the animation to the running animation you are using but you can probably figure it out since you are an aspiring scripter!
##The Solution
Since client movement is replicated to the server, we can change the walkspeed on the client!
(This is going to be a rather semi-lengthy post; if you just want the code and not an explanation all the code is at the bottom as well an uncopylocked place you can look at.)
###Setup
We can insert a "LocalScript" instance into StarterCharacterScripts which is located in the StarterPlayer folder in the explorer. We can name it whatever we want but I am going to name it "ShiftRun"
###The Code
So we will want to define the player's service so we can index the local player and eventually their humanoid via their character. We can use the :GetService method of game to get a service, we just need to pass the name of the service. Example game:GetService("Players").
Now that we know how to get a service such as players we can learn how to index the local player. You need to be on the client in a localscript to index LocalPlayer! LocalPlayer is a property of the players service so we just set a variable and index .LocalPlayer There is a .Character property of a player instance which is reference to the player's character. (Note this is sometimes nil so we can yield until it exists if it doesn't already exist. client.Character or client.CharacterAdded:Wait()) Then we wait for Humanoid to exist inside of the character so we can change the WalkSpeed property. We will also want to define the UserInputService service since we will be using the Shift key to activate the running which is an input from the user. Also set a variable for the sprintspeed of the player.
local userInputService = game:GetService("UserInputService")
local players = game:GetService("Players")
local client = players.LocalPlayer
local character = client.Character or client.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local sprintSpeed = 30
We have our variables setup, now lets set up our functions to make the variables useful.
We will be using the InputBegan signal of UserInputService which we can connect a function to using the :Connect method. Our connected function will have two arguments automatically passed. The first being the input of the key or action the player did. The second being if the player is typing in something like chat. (Some people refer to this as the "GameProcessedEvent" or "GPE/gpe") We will use both of these because we want to know if the player is holding shift as well if they are typing or not so we don't disturb them when they are typing in chat!
The framework for our function will be something like this.
local function userInputBegan(input, isTyping)
if isTyping then return end
end
userInputService.InputBegan:Connect(userInputBegan)
Now we have the basics of the function we can start to make the actual sprinting. We will want to check if the input's keycode (Input.KeyCode) is the KeyCode.LeftShift enum. If the input's keycode is shift then we keep looping the humanoid's walkspeed to be our sprinting speed which we can define in a variable. We will use the :IsKeyDown method of UserInputService for our condition whether they stopped running or not. We will want to add some sort of yield in the loop to prevent us from filling up memory and crashing the game. Such example of a yield is wait() which will yield for ~0.03 seconds is no argument is passed. We can set the WalkSpeed variable to our sprintingSpeed and then after the while loop has finished we can reset the speed to the default speed.
local sprintSpeed = 30
local defaultWalkspeed = 16
local function userInputBegan(input, isTyping)
if isTyping then return end
if input.KeyCode == Enum.KeyCode.LeftShift then
while userInputService:IsKeyDown(Enum.KeyCode.LeftShift) do
humanoid.WalkSpeed = sprintSpeed
wait()
end
humanoid.WalkSpeed = defaultWalkspeed
end
end
###The Final Code
local userInputService = game:GetService("UserInputService")
local players = game:GetService("Players")
local client = players.LocalPlayer
local character = client.Character or client.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local sprintSpeed = 30
local defaultWalkspeed = 16
local function userInputBegan(input, isTyping)
if isTyping then return end
if input.KeyCode == Enum.KeyCode.LeftShift then
while userInputService:IsKeyDown(Enum.KeyCode.LeftShift) do
humanoid.WalkSpeed = sprintSpeed
wait()
end
humanoid.WalkSpeed = defaultWalkspeed
end
end
userInputService.InputBegan:Connect(userInputBegan)
##Extra
An uncopylocked place with all the source code used can be found here.
6 comments
KeyDown is deprecated, use UserInputService instead
Additionally, you should not be handling user input server side
Use UserInputService, and use this on LocalScript
That is literally what I said
"Shift" is not a key. its a different term
You need to do this from a localscript since
GetMousemethod can only be called from the client.