I can't change the walkspeed when a player is already sprinting

I am trying to make it so when an Xbox player clicks a button, it makes their sprint toggle. However, if they are already sprinting and they press the button, it keeps them sprinting.

Button script:

local buttonPart = script.Parent
local clickDetector = buttonPart:FindFirstChild("ClickDetector")

local function onClick(player)
	local character = player.Character
	if character then
		-- Find the Humanoid in the player's character
		local humanoid = character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid.WalkSpeed = 16  -- Set the player's walk speed to 16
			
			
			
			--turn of the sprint button and enable the toggle
			local xboxSprintButton = character:FindFirstChild("XboxSprintButton")
			
			local xboxSprintButtonHold = character:FindFirstChild("XboxSprintButtonHold")

			if xboxSprintButton then
				xboxSprintButton.Enabled = false
			end

			if xboxSprintButtonHold then
				xboxSprintButtonHold.Enabled = true
			end
			-- Update the stored walk speed attribute
			
			
			
			humanoid:SetAttribute("OriginalWalkSpeed", 16)
		end
	end
end

clickDetector.MouseClick:Connect(onClick)

Sprint script (Local script in StarterCharacterScripts)

local buttonPart = script.Parent
local clickDetector = buttonPart:FindFirstChild("ClickDetector")

local function onClick(player)
	local character = player.Character
	if character then
		-- Find the Humanoid in the player's character
		local humanoid = character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid.WalkSpeed = 16  -- Set the player's walk speed to 16
			
			
			
			--turn of the sprint button and enable the toggle
			local xboxSprintButton = character:FindFirstChild("XboxSprintButton")
			
			local xboxSprintButtonHold = character:FindFirstChild("XboxSprintButtonHold")

			if xboxSprintButton then
				xboxSprintButton.Enabled = false
			end

			if xboxSprintButtonHold then
				xboxSprintButtonHold.Enabled = true
			end
			-- Update the stored walk speed attribute
			
			
			
			humanoid:SetAttribute("OriginalWalkSpeed", 16)
		end
	end
end

clickDetector.MouseClick:Connect(onClick)

2 Likes