Make player not sprint if only shift is pressed

robloxapp-20231126-0739260.wmv (724.1 KB)
How would i make the player not run if only the shift button is pressed, here’s the script that i made:

local UIs = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local Camera = game.Workspace.CurrentCamera
local localPlayer = Players.LocalPlayer
local Animsfolder = game:GetService("ReplicatedStorage").Anims
local runanim = Animsfolder.RunAnim
--
local character
local Humanoid
local Ranimtrack
local canrun
--dipake nanti
local Info = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local Properties = { FieldOfView = 80 }
local T = game:GetService("TweenService"):Create(Camera, Info, Properties)
local rProperties = { FieldOfView = 70 }
local rInfo = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local rT = game:GetService("TweenService"):Create(Camera, rInfo, rProperties)
--sprint settings
local BoolValue = false
local Rspeed = 32
local Nspeed = 16
--

local function LoadAnimation(char)
	character = char
	Humanoid = character:WaitForChild("Humanoid")
	Ranimtrack = Humanoid:LoadAnimation(runanim)
end

if player.Character then
	LoadAnimation(player.Character)
end	
player.CharacterAdded:Connect(LoadAnimation)

local function Toggle()
	BoolValue = not BoolValue
	if BoolValue then
		T:Play()
		Ranimtrack:Play()
		repeat wait() until not BoolValue
	else
		rT:Play()
		Ranimtrack:Stop()
		repeat wait() until BoolValue
	end
end


UIs.InputBegan:Connect(function(input,gameProcessed)
	if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
		Humanoid.WalkSpeed = Rspeed
		Toggle()
	end
end)

UIs.InputEnded:Connect(function(input,gameProcessed)
	if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
		Humanoid.WalkSpeed = Nspeed
		Toggle()
	end
end)

Any help would be appreciated

The Magnitude property of the MoveDirection property of the Humanoid tells you if the player is moving or not. Add this to the bottom of your script.

local function moveDirectionChanged()
    local magnitude = Humanoid.MoveDirection.Magnitude -- Get the magnitude
    if magnitude == 0 then
        -- Player is not moving
        if not BoolValue then
            Humanoid.WalkSpeed = Nspeed
            Toggle()
        end
    end
end

Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(moveDirectionChanged)
1 Like

it says this Players.aryaanggahiro.PlayerScripts.Sprint:76: attempt to index nil with 'GetPropertyChangedSignal'

im a lil goofy but i would either put

repeat wait() until Humanoid --wait for humanoid

before the last line

or move the moveDirectionChanged function to somewhere above

if player.Character then
	LoadAnimation(player.Character)
end	

and then move

Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(moveDirectionChanged)

inside the aforementioned if statement

im a little confused about which “last line” you mean, is it the function’s last line or the entire script?

Shift may not be the best choice for this, it also changes camera view point.

mb I meant

Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(moveDirectionChanged)

which I assumed was at the bottom of ur script

soo… i changed the script into this :

local UIs = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local Camera = game.Workspace.CurrentCamera
local localPlayer = Players.LocalPlayer
local Animsfolder = game:GetService("ReplicatedStorage").Anims
local runanim = Animsfolder.RunAnim
--
local character
local Humanoid
local Ranimtrack
local canrun
--dipake nanti
local Info = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local Properties = { FieldOfView = 80 }
local T = game:GetService("TweenService"):Create(Camera, Info, Properties)
local rProperties = { FieldOfView = 70 }
local rInfo = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local rT = game:GetService("TweenService"):Create(Camera, rInfo, rProperties)
--sprint settings
local BoolValue = false
local Rspeed = 32
local Nspeed = 16
--

local function Toggle()
	BoolValue = not BoolValue
	if BoolValue then
		T:Play()
		Ranimtrack:Play()
		repeat wait() until not BoolValue
	else
		rT:Play()
		Ranimtrack:Stop()
		repeat wait() until BoolValue
	end
end


local function moveDirectionChanged()
	local magnitude = Humanoid.MoveDirection.Magnitude -- Get the magnitude
	if magnitude == 0 then
		-- Player is not moving
		if not BoolValue then
			Humanoid.WalkSpeed = Rspeed
			Toggle()
		end
	end
end

local function LoadAnimation(char)
	character = char
	Humanoid = character:WaitForChild("Humanoid")
	Ranimtrack = Humanoid:LoadAnimation(runanim)
end

if player.Character then
	LoadAnimation(player.Character)
end	
player.CharacterAdded:Connect(LoadAnimation)

UIs.InputBegan:Connect(function(input,gameProcessed)
	if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
		Humanoid.WalkSpeed = Rspeed
		Toggle()
	end
end)

UIs.InputEnded:Connect(function(input,gameProcessed)
	if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
		Humanoid.WalkSpeed = Nspeed
		Toggle()
	end
end)

repeat wait() until Humanoid
Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(moveDirectionChanged)

and the sprint system… still has an error
robloxapp-20231126-1612504.wmv (739.8 KB)

2 Likes

can you show the console pls so we can see if there are errors

(find it in the roblox game menu or type “/console” without the quotes into the chat)

Is this in StarterCharacterScripts?

it’s in starterplayerscripts
image