Sprint Fix Help

so what im trying to do is make it so even if your still holding the sprint button if it detects a tag called NoRun on ur character it cancels it this is a local script in startercharacterscripts

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local anim = script.RunAnim
local animTrack = humanoid.Animator:LoadAnimation(anim)

local camera = game.Workspace.CurrentCamera

local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

local Configuration = require(game.ReplicatedStorage:WaitForChild("Configuration"))

function IsWalking()
	if humanoid.MoveDirection.Magnitude > 0 then
		return true
	else
		return false
	end
end


UIS.InputBegan:Connect(function(key, proccess)
	if not character:HasTag("NoRun") then
		if key.KeyCode == Configuration.RunKey then
		if IsWalking() then
			humanoid.WalkSpeed = Configuration.RunSpeed
			TweenService:Create(camera, TweenInfo.new(Configuration.TweenFovDuration, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {FieldOfView = Configuration.RunFov}):Play()
			animTrack:Play()
		end
	end
	end
end)

UIS.InputEnded:Connect(function(key, proccess)
	if key.KeyCode == Configuration.RunKey then
		humanoid.WalkSpeed = Configuration.WalkSpeed
		TweenService:Create(camera, TweenInfo.new(Configuration.TweenFovDuration, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {FieldOfView = Configuration.WalkFov}):Play()
		animTrack:Stop()
	end
end)

2 Likes

Since tags aren’t as easy to use compared to other methods (at least in my experience), I’d recommend you turning NoRun into a bool instead. If you’re cool with this, this script should work:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local anim = script.RunAnim
local animTrack = humanoid.Animator:LoadAnimation(anim)

local camera = game.Workspace.CurrentCamera

local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

local Configuration = require(game.ReplicatedStorage:WaitForChild("Configuration"))

-- Shortened this bit, no need for an else statement.
function IsWalking()
	if humanoid.MoveDirection.Magnitude > 0 then
		return true
	end
	return false
end

function sprint()
	if character:FindFirstChild("NoRun").Value then
		if IsWalking() then
			humanoid.WalkSpeed = Configuration.RunSpeed
			TweenService:Create(camera, TweenInfo.new(Configuration.TweenFovDuration, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {FieldOfView = Configuration.RunFov}):Play()
			animTrack:Play()
		else
			warn("Player not walking.")
		end
	else
		warn("NoRun is true!")
	end
end

function sprintStop()
	humanoid.WalkSpeed = Configuration.WalkSpeed
	TweenService:Create(camera, TweenInfo.new(Configuration.TweenFovDuration, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {FieldOfView = Configuration.WalkFov}):Play()
	animTrack:Stop()
end

UIS.InputBegan:Connect(function(key, gp)
	if key.KeyCode == Configuration.RunKey and not gp then
		if character:FindFirstChild("NoRun") then
			sprint()
		else
			warn("NoRun not found! Adding now...")
			local NoRun = Instance.new("BoolValue")
			NoRun.Value = false
			NoRun.Name = "NoRun"
			NoRun.Parent = character
			sprint()
		end
		
		-- When NoRun is true, sprint stops.
		character.NoRun:GetPropertyChangedSignal("Value"):Connect(function()
			if character.NoRun.Value then
				sprintStop()
			end
		end)
		
		-- If its removed, removes sprinting functionality. Should discourage hacking.
		character.ChildRemoved:Connect(function(child)
			if child.Name == "NoRun" and child:IsA("BoolValue") then
				sprintStop()
			end
		end)
	end	
end)

UIS.InputEnded:Connect(function(key, gp)
	if key.KeyCode == Configuration.RunKey and not gp then
		sprintStop()
	end	
end)

You can add a listener at the bottom of your script to stop the running when the tag is added.

local CollectionService = game:GetService("CollectionService")

CollectionService:GetInstanceAddedSignal("NoRun"):Connect(function(instance)
	if instance == character and humanoid.WalkSpeed == Configuration.RunSpeed then
		humanoid.WalkSpeed = Configuration.WalkSpeed
		TweenService:Create(camera, TweenInfo.new(Configuration.TweenFovDuration, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {FieldOfView = Configuration.WalkFov}):Play()
		animTrack:Stop()
	end
end)

sorry for the late response but this just makes it so i cant sprint at all for some reason ill dig in the code a bit more and see

this did work tho W thanks man

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.