Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I prevent this sprint function from firing twice? [SOLVED]

So I've this sprint script that just makes a character sprint. However, sometimes it drains twice as much stamina as it should. I'm pretty sure it's because the two ways the sprint function can be fired fire at the same time. How do I prevent this?

Here's the script (LocalScript in a ScreenGui in the StarterGui)

local ts = game:GetService("TweenService")
local ti = TweenInfo.new(.15,Enum.EasingStyle.Linear)
local rs = game:GetService("ReplicatedStorage")
local stamina = 100
local draining = false

local player = game.Players.LocalPlayer
local character = player.Character
if character == nil then
	player.CharacterAdded:Wait()
	character = player.Character
end

local sprinting = false
local anim = rs:WaitForChild("Sprint")
local sprintanim = character:WaitForChild("Humanoid"):LoadAnimation(anim)
local staminagui = player:WaitForChild("PlayerGui"):WaitForChild("StaminaGui"):WaitForChild("Frame")

local function stopsprint()
	if sprinting == true then
		sprinting = false
		local tween = ts:Create(game.Workspace.CurrentCamera, ti, {FieldOfView = 70})
		tween:Play()
		local tween2 = ts:Create(character.Humanoid, ti, {WalkSpeed = 16})
		tween2:Play()
		sprintanim:Stop()
		repeat
			if stamina < 100 then
				stamina = stamina + 1
				staminagui.StamNum.Text = stamina .. " / 100"
				if stamina > 25 then
					staminagui:WaitForChild("StamNum").TextColor3 = Color3.new(0,0,0)
					character.Humanoid.JumpPower = 50
				end
				if stamina <= 25 then
					staminagui.StamNum.TextColor3 = Color3.new(255/255, 0/255, 0/255)
					character.Humanoid.JumpPower = 0
				end
			end
			wait(0.1)
		until
			sprinting == true
	end
end
local function sprint()
	if sprinting == false and character.Humanoid.PlatformStand == false and stamina > 25 then
		sprinting = true
		local tween = ts:Create(game.Workspace.CurrentCamera, ti, {FieldOfView = 90})
		tween:Play()
		sprintanim:Play()
		local tween2 = ts:Create(character.Humanoid, ti, {WalkSpeed = 24})
		tween2:Play()
		repeat
			draining = true
			stamina = stamina - 1
			staminagui.StamNum.Text = stamina .. " / 100"
			if stamina > 25 then
				staminagui:WaitForChild("StamNum").TextColor3 = Color3.new(0,0,0)
				character.Humanoid.JumpPower = 50
			end
			if stamina <= 25 then
				staminagui.StamNum.TextColor3 = Color3.new(255/255, 0/255, 0/255)
				character.Humanoid.JumpPower = 0
			end
			if stamina == 0 then
				stopsprint()
			end
			wait(0.1)
			draining = false
		until
		sprinting == false
	end
end

local moving = false
local hittingshift = false
character:WaitForChild("Humanoid").Running:Connect(function(speed)
	if speed > 0 then
		moving = true
		if hittingshift == true then
			sprint()
		end
	else
		moving = false
		stopsprint()
	end
end)

game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.LeftShift then
		hittingshift = true
		if moving == true then
			sprint()
		end
    end 
end)

game:GetService("UserInputService").InputEnded:connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.LeftShift then
		hittingshift = false
        stopsprint()
    end 
end)	

1 answer

Well, there are two solutions to your problem. Either, remove sprint() and stopsprint() from UserInputService, or, remove sprint() and stopsprint() from Humanoid.Running event. You might be thinking that removing sprint() and stopsprint() from Humanoid.Running might make the player sprint while standing, but you have an if statement, character.Humanoid.PlatformStand == false.

Here's the fixes [Do either one of them] :

No. 1 :

local moving = false
local hittingshift = false
character:WaitForChild("Humanoid").Running:Connect(function(speed)
    if speed > 0 then
        moving = true
    else
        moving = false
    end
end)

game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.LeftShift then
        hittingshift = true
        if moving == true then
            sprint()
        end
    end 
end)

game:GetService("UserInputService").InputEnded:Connect(function(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.LeftShift then
        hittingshift = false
        stopsprint()
    end 
end)  

No. 2:

local moving = false
local hittingshift = false
character:WaitForChild("Humanoid").Running:Connect(function(speed)
    if speed > 0 then
        moving = true
        if hittingshift == true then
            sprint()
        end
    else
        moving = false
        stopsprint()
    end
end)

game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.LeftShift then
        hittingshift = true
    end 
end)

game:GetService("UserInputService").InputEnded:Connect(function(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.LeftShift then
        hittingshift = false
    end 
end)  

Make sure of capitalisation!

Lemme know if t helps!

4 comments

0

The thing is, when the sprint() function is removed from either places where it is, either hitting shift while standing still, then moving does nothing, or hitting shift while moving does nothing.

0

Well, try adding prints and also prints for boolean. And tell what it prints.

0

Nevermind, I got it to work

0

It will be helpful for future users with identical problem, if you edit to what you done to fix your isue.

This discussion is preserved as it was. New answers, comments, and votes are closed.