My sprint script doesn't work

Hello! So, I’ve grabbed a generic shift to sprint script (don’t judge me, please), and I tried to customize it a bit, however, I’ve ran into this problem (I do not get an error).

Sprint script:
local mouse = game.Players.LocalPlayer:GetMouse()
local running = false
local remote = game.ReplicatedStorage:WaitForChild(“raceCheck”)

function getTool()	
	for _, kid in ipairs(script.Parent:GetChildren()) do
		if kid.className == "Tool" then return kid end
	end
	return nil
end


    mouse.KeyDown:connect(function (key) -- Run function
    	key = string.lower(key)
    	if string.byte(key) == 48 then
    		running = true
    		local keyConnection = mouse.KeyUp:connect(function (key)
    			if string.byte(key) == 48 then
    				running = false
    			end
    		end)
    		for i = 1,5 do
    			
    			wait()
    		end
    		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 25
    		repeat wait () until running == false
    		keyConnection:disconnect()
    		remote:FireServer()
    		for i = 1,5 do
    			
    			wait()
    		end
    	end
    end)

The remote is to check if a player’s race let’s them return to their normal speed, or their boosted speed (if they’re that race):

local remoteEvent = game.ReplicatedStorage:WaitForChild("raceCheck")

remoteEvent.OnServerEvent:Connect(function(plr)
	if plr.race.Value == 10 then
		plr.Character.Humanoid.WalkSpeed = 18
		print("arachnis")
	else
		plr.Character.Humanoid.WalkSpeed = 16
		print("not arachnis")
	end
end)

It prints, however, here, we come to the issue, the shift to sprint script breaks, and the player keeps their sprinting walkspeed forever (until they reset).

Any ideas on how to fix this?

I wouldn’t make it a tool. there are actually really good sprint scripts in toolbox, and you can edit the walkspeed to how you want

Hello!

Your code seems interesting, but I would make it a LocalScript in a GUI or just inside the player’s PlayerGui. I would go for something like this:

LocalScript in a GUI or PlayerGui
local Player = game.Players.LocalPlayer
local UserInputService = game:GetService(“UserInputService”)

local sprintEvent = --The RemoteEvent you would be firing when the player has pressed the sprint key
--You could go for ReplicatedStorage or a folder called "Events" to keep it more organised.

UserInputService.InputBegan:Connect(function(key))
if key == Enum.KeyCode.(whatever your sprint key would be, remove the parentheses as well) then
sprintEvent:FireServer("Sprint")
end
end)

UserInputService.InputEnded:Connect(function(key))
if key == Enum.KeyCode.(whatever your sprint key would be, remove the parentheses as well) then
sprintEvent:FireServer("Walk")
end
end)

Server Script in ServerScriptService
local sprintEvent = --Where you have located the RemoteEvent

sprintEvent.OnServerEvent:Connect(function(Player, Type))
if Type == "Sprint" then
Player.Character.Humanoid.WalkSpeed = 25
elseif Type == "Walk" then
Player.Character.Humanoid.WalkSpeed = 16 --You could edit this part to make it compatible with your racing feature
end
end)

The thing with this method is that it is not compatible with mobile or console devices, so I would suggest you to read through this:

You could also just make a ScreenGui or add a TextButton / ImageButton, but it is more advisable to just use InputService as it will probably make it more accessable to any devices available.

I hope this helped, I didn’t write any of the code in Roblox Studio, so please proceed with caution just in case something’s not working properly.

And please note that this is how I would go about this, others may have other ways of doing this. I just felt like helping you in case you were really stuck.

Have a great day!