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

Struggling with making a sprint function on mobile?

Hello, I am making a sprint button for mobile and XBOX. The idea is that when you HOLD the sprint button on mobile, or hold the thumbstick or hold L Shift then you sprint until you either run out of energy or you release the button.

The problem is that I have made the code and it doesn't do anything and I am not sure why. There are no errors because the script works but not properly. Please help me this is really important.

local contextActionService = game:GetService("ContextActionService")
local UIS = game:GetService('UserInputService')

local Bar = script.Parent:WaitForChild('Background'):WaitForChild('Bar')

local player = game:GetService'Players'.LocalPlayer

local NormalWalkSpeed = 16
local NewWalkSpeed = 32

local power = 10

local sprinting = false

repeat wait() until game.Players.LocalPlayer.Character

local character = player.Character

function onButtonPress(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin  then
        character.Humanoid.WalkSpeed = NewWalkSpeed
        sprinting = true
        while power > 0 and sprinting do
            power = power - .03
            Bar:TweenSize(UDim2.new(power / 10, 0, 1, 0), 'Out', 'Quint', .1, true)
            --Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 42, 42), 0.001)
            wait()
            if power <= 0 then
                character.Humanoid.WalkSpeed = NormalWalkSpeed
    else
        local character = player.Character or player.CharacterAdded:Wait()
        character.Humanoid.WalkSpeed = NormalWalkSpeed
                        sprinting = false
                        while power < 10 and not sprinting do
                            power = power + .03
                            Bar:TweenSize(UDim2.new(power / 10, 0, 1, 0), 'Out', 'Quint', .1, true)
                            --Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 166, 11), 0.001)
                            wait()
                            if power <= 0 then
                        character.Humanoid.WalkSpeed = NormalWalkSpeed
                        end
                end
            end
        end
    end
end

local mobilebutton = contextActionService:BindAction("SprintButton",onButtonPress,true,Enum.KeyCode.LeftShift, Enum.KeyCode.Thumbstick1)
contextActionService:SetPosition("SprintButton",UDim2.new(0.72,-25,0.20,-100))
contextActionService:SetImage("SprintButton", "http://www.roblox.com/asset/?id=5632374236")

1 answer

✓ Accepted answer

I think your best approach would be to handle the sprinting inside of a renderstepped function, and use your OnButtonPress function to determine if they are holding the sprint button or not.

local contextActionService = game:GetService("ContextActionService")
local UIS = game:GetService('UserInputService')


local player = game:GetService'Players'.LocalPlayer

local WalkSpeed = 16
local SprintSpeed = 32

local power = 10 -- How much power the player currently has
local maxpower = 10 -- How much power the player can have

local character = player.Character
local holding = false
local Bar = script.Parent:WaitForChild('Background'):WaitForChild('Bar')

repeat wait() until game.Players.LocalPlayer.Character
game:GetService("RunService").RenderStepped:Connect(function(ticks) -- Renderstepped has a parameter for how long it took inbetween steps (in this case ticks). We can use this to determine how much power to remove/add
	local differencepersecond = 2 -- Changing 2 will modify how fast the power drains/increases. Using 2 will take 5 seconds to remove 10 power. E.g: Using 1 will take 10 seconds, 2 takes 5, 3 takes 3.33, etc. Same applies to adding
	local timer = ticks * differencepersecond -- Formula to determine how fast. Modify above line instead of this one.
	if holding == true and power > 0 then -- Detect if sprint key/button is held, and if they have enough power
		character.Humanoid.WalkSpeed = SprintSpeed -- Change to sprint speed
		power = power - timer -- Remove some power
		if power <= 0 then -- Make sure they didnt run out of power
			holding = false -- If so, cancel sprinting
		end
	else -- If key isn't held, or not enough power
		character.Humanoid.WalkSpeed = WalkSpeed -- Change to walk speed
		if power < maxpower then -- Make sure they have less power than the max
			power = power + timer  -- if so add some power. If you want power to be added at a different speed than it was removed, instead of power + timer, do power + (ticks * AMOUNTPERSEC)
			if power > maxpower then power = maxpower end -- Make sure they dont go over max power
			end
	end
	Bar:TweenSize(UDim2.new(power / maxpower, 0, 1, 0), 'Out', 'Quint', .1, true) -- Since this just resizes a bar to show you how much power you have, you can leave it at the end of the stepped function so it updates once, and is always accurate.
end)
function onButtonPress(actionName, inputState, inputObject)
	if inputState == Enum.UserInputState.Begin  then -- Use begin to detect when the key was pressed
		holding = true -- Detect that key as being held down
	elseif inputState == Enum.UserInputState.End  then -- Use end to detect when key was released
		holding = false -- If key was released, they aren't holding it anymore.
	end
end

local mobilebutton = contextActionService:BindAction("SprintButton",onButtonPress,true,Enum.KeyCode.LeftShift, Enum.KeyCode.Thumbstick1)
contextActionService:SetPosition("SprintButton",UDim2.new(0.72,-25,0.20,-100))
contextActionService:SetImage("SprintButton", "http://www.roblox.com/asset/?id=5632374236")

The problem with your code was your use of loops. They were causing eachother not to run/stay running, and just generally canceling each other out. It was a bit of a mess but you were on the right track, I just think you would fair better with this code layout.

3 comments

0

Thank you so much! Im so relieved that I was on the right track :D

0

No problem :) Happy coding

0

Thanks you too :)

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