How do I make a mobile sprint button with CAS that doesn't constantly stop sprinting when I move my thumb?

I am using CAS to make a mobile sprint button, which uses Left Shift. It works perfectly on PC but on mobile the moment you move your thumb even slightly, even by a single pixel and when it’s still on the button, it stops sprinting and seems to think contact has been ended? How would I make it so it sprints while it’s being held down?

Could you show me the script, please?

1 Like

Hi, here is the script

local function handleContext(name, state, input)
if stamina.Value > 0 and isSprinting.Value == false then
	isSprinting.Value = true
	re:FireServer(sprintSpeed)				
	repeat
		stamina.Value = stamina.Value - 1
		wait()
	until stamina.Value == 0 or state == Enum.UserInputState.End or isSprinting.Value == false
	isSprinting.Value = false
	re:FireServer(walkSpeed)
else
	isSprinting.Value = false
	re:FireServer(walkSpeed)
end	
end

Probably pretty messy since I was basically just trying lots of different things, but I am still not sure what the best way to get it to work while being held down is

Try this:

local function handleContext(name, state, input)
	if stamina.Value > 0 and isSprinting.Value == false then
		if state == Enum.UserInputState.Begin then
			isSprinting.Value = true
			re:FireServer(sprintSpeed)
			repeat
				stamina.Value -= 1
				wait()
			until stamina.Value == 0 or isSprinting.Value == false
			if isSprinting.Value == true then
				isSprinting.Value = false
				re:FireServer(walkSpeed)	
			end
		elseif state == Enum.UserInputState.End then
			isSprinting.Value = false
			re:FireServer(walkSpeed)
		end
	else
		re:FireServer(walkSpeed)
	end
end
1 Like

Thank you, that was definitely the closest so far. Unfortunately it started to bug on PC (isSprinting.Value would get set to true and wouldn’t activate the first if statement), so I added “isSprinting.Value = false” in the final else statement, which then broke it on mobile. I ended up removing the “and isSprinting.Value == false” as well and it now works. Only problem is if you hold down the button and then drag your thumb off it, you will continue to sprint, but I think I can live with that. Thank you for the help!

local function handleContext(name, state, input)
	if stamina.Value > 0 then
		if state == Enum.UserInputState.Begin then
			isSprinting.Value = true
			re:FireServer(sprintSpeed)				
    		repeat
    			stamina.Value -= 1
 				wait()
			until stamina.Value == 0 or isSprinting.Value == false
			if isSprinting.Value == true then
				isSprinting.Value = false
				re:FireServer(walkSpeed)
			end
		elseif state == Enum.UserInputState.End then
			isSprinting.Value = false
			re:FireServer(walkSpeed)
		end	
	else
		isSprinting.Value = false
		re:FireServer(walkSpeed)	
	end
end
1 Like