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

So... Here is my sprint script it is somehow working buti feel like that it can be better?

local mouse = game.Players.LocalPlayer:GetMouse()
local running = false

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)
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = game.Players.LocalPlayer.Stats.Agility.Value + 30
		repeat wait () until running == false or game.Players.LocalPlayer.Stamina.Value <= 0
		keyConnection:disconnect()
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
	end
end)

mouse.KeyDown:connect(function (key)
	key = string.lower(key)
	if string.byte(key) == 48 then
		running = true
		while running == true and game.Players.LocalPlayer.Stamina.Value > 0 do
			game.Players.LocalPlayer.Stamina.Value = game.Players.LocalPlayer.Stamina.Value-2
			print("Gay")
			if game.Players.LocalPlayer.Stamina.Value <= 2 then
				game.Players.LocalPlayer.Stamina.Value = 0
			end
			wait(0.1)
		end
		running = false
	end
end)

It sprints based on player's agility and drains the player's stamina by 2 every 1/10th of a second(second block of code)

Experimented until it worked, but I have a strong feeling that there will be a bug somewhere and it can be improved

1 comment

0

You shouldn’t be using KeyDown.

1 answer

✓ Accepted answer

###There won’t be a bug, but you shouldn’t be using KeyDown to get user input. It’s deprecated and just so inefficient. Instead, use UserInputService.InputBegan.

local userInputService = game:GetService"UserInputService"
local plr = game:GetService"Players".LocalPlayer

userInputService.InputBegan:Connect(function(key, chatting)
    if chatting then
        return
    end

    if key.KeyCode == Enum.KeyCode.LeftShift then
        plr.Character.Humanoid.WalkSpeed = 24 -- set to desired speed
    end
end)

userInputService.InputEnded:Connect(function(key, chatting)
    if chatting then
        return
    end
    
    if key.KeyCode == Enum.KeyCode.LeftShift then
        plr.Character.Humanoid.WalkSpeed = 16
    end
end)

3 comments

0

Should be InputEnded for the second one.

0

forgot lol

0

Thanks, modified the script a little bit to make it drain stamina and stop on stamina. Thanks xD

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