Help with sprint button activation script

I wanted to create sprint activation/deactivation at the click of a button. The problem is that in the function, the second condition does not work.

local UserInputService = game:GetService("UserInputService")
local sprintButton = script.Parent.sprint
local sprintActive = script.Parent.sprint_active
local jumpButton = script.Parent.jump

local keyPressed = false
local humanoid = nil

if UserInputService.TouchEnabled then
	script.Parent.Enabled = true
else
	script.Parent.Enabled = false
end

sprintButton.MouseButton1Click:Connect(function()
	
		if game.Players.LocalPlayer.Character then
			humanoid = game.Players.LocalPlayer.Character:FindFirstChildOfClass("Humanoid")
		end

		if humanoid and not keyPressed then
			print("on")
			keyPressed = true
			humanoid.WalkSpeed = 33.98 
			sprintActive.Visible = true
		elseif humanoid and keyPressed then
			print("off")
			keyPressed = false
			humanoid.WalkSpeed = 17.5
			sprintActive.Visible = false
		end
end)


1 Like

What is the second condition? Please, elaborate more.

only the first part “if humanoid and not Typed” in the function is always triggered, which checks the value of the typed variable

try printing “Humanoid”. You’re weirdly setting the humanoid object.

1 Like

I have found a solution. I have not written a function for the second sprintActive button. Thank you anyways!

1 Like

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