Hey everyone,
I am making a sprint mechanism for both PC and mobile using ContextActionService. It actually works pretty well so far, but there is a major issue (for mobile). The issue is that whenever you move your thumb even the slightest pixel when holding the button, you stop sprinting. I will attach the code and a video to explain the problem.
Thanks in advance,
Mustang
if key.KeyCode == Enum.KeyCode.LeftShift or key.KeyCode == Enum.KeyCode.RightShift then
running = true
end
end)
local ContextActionService = game:GetService"ContextActionService"
local LocalPlayer = game:GetService"Players".LocalPlayer
ContextActionService:BindAction("Sprint",function(name,state,input)
if state == Enum.UserInputState.Begin then
local char = LocalPlayer.Character
if char then
running = true
end
else
local char = LocalPlayer.Character
if char then
running = false
end
end
return Enum.ContextActionResult.Pass
end,true,Enum.KeyCode.LeftShift,Enum.KeyCode.RightShift)
humanoid.Running:connect(function(Speed)
ContextActionService:SetPosition("Sprint",UDim2.new(0,0,0.5,0))
ContextActionService:SetTitle("Sprint","Sprint")
ContextActionService:GetButton("Sprint").Size = UDim2.new(0.5,0,0.5,0)
end)
humanoid.Running:connect(function(Speed)
if Speed >= 1 and running and not RAnimation.IsPlaying then
RAnimation:Play()
elseif Speed >= 1 and not running and RAnimation.IsPlaying then
RAnimation:Stop()
elseif Speed < 1 and RAnimation.IsPlaying then
RAnimation:Stop()
end
end)
humanoid.Changed:connect(function()
if humanoid.Jump and RAnimation.IsPlaying then
RAnimation:Stop()
end
end)
game:GetService("UserInputService").InputEnded:Connect(function(key)
if key.KeyCode == Enum.KeyCode.LeftShift or key.KeyCode == Enum.KeyCode.RightShift then
running = false
end
end)
while wait() do
if enabled.Value == true then
if humanoid then
if humanoid.MoveDirection == Vector3.new(0,0,0) then moving = false else moving = true end
if tired then
ChangeSpeed(exhaust.Value) -- Rest
Frame.Rest1.Visible = true
Frame.Rest2.Visible = true
if not moving then
stamina = stamina + 1
end
if stamina > 0 then
tired = false
Frame.Rest1.Visible = false
Frame.Rest2.Visible = false
Frame.GreenBar:TweenSize(UDim2.new(3.5*stamina/875,0, 0,25), "Out", "Quad", 0.1, true)
end
elseif running == true then
if moving then
ChangeSpeed(run.Value) -- Sprint
stamina = stamina - 1
Frame.GreenBar:TweenSize(UDim2.new(3.5*stamina/875,0, 0,25), "Out", "Quad", 0.1, true)
if stamina == 0 then tired = true stamina = -10 end
elseif stamina < 250 then
stamina = stamina + 1
Frame.GreenBar:TweenSize(UDim2.new(3.5*stamina/875,0, 0,25), "Out", "Quad", 0.1, true)
end
else ChangeSpeed(walk.Value) -- Walk
if not moving and stamina < 250 then
stamina = stamina + 1
Frame.GreenBar:TweenSize(UDim2.new(3.5*stamina/875,0, 0,25), "Out", "Quad", 0.1, true)
end
end
end
end
end
Video: