I cant get it to work! [RunningScript]

So technically, I got this script that once you press W (on pc) it speeds up and animations plays. But on mobile it doesnt work so how do i make the script run for mobile players?

–Please find me a solution to my problem, my brain is going to explode :slightly_smiling_face:
Thanks, Both :slight_smile:

Code:

local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character

local maxSpeed = 35 
local maxJump = 20 
local acceleration = 0.08 

local currentSpeed = 18
local currentJump = 30
local currentAnimSpeed = 0.4
local isAccelerating = false
local isRunning = false

local runSound = game.SoundService.Running
runSound.PlaybackSpeed = currentSpeed / maxSpeed 

local function increaseSpeed()
	if currentSpeed < maxSpeed then
		currentSpeed = currentSpeed + acceleration
		currentAnimSpeed = currentAnimSpeed + (acceleration / maxSpeed)
	else
		currentSpeed = maxSpeed
		currentAnimSpeed = 1.1
		isAccelerating = false
	end

	Character.Humanoid.WalkSpeed = currentSpeed
	Character.Humanoid.JumpPower = currentSpeed * 1.18 -- Increase jump power with speed
	PlayAnim:AdjustSpeed(currentAnimSpeed)
	runSound.PlaybackSpeed = currentSpeed / maxSpeed -- Adjust playback speed based on current speed
end

local function increaseJump()
	if currentJump < maxJump then
		currentJump = currentJump + acceleration
	else
		currentJump = maxJump
		currentAnimSpeed = 1.1
		isAccelerating = false
	end

	Character.Humanoid.JumpPower = currentJump
end

function ToggleAnimationAndSound(shouldPlay)
	if shouldPlay then
		if not PlayAnim.IsPlaying then
			PlayAnim:Play()
		end
		if not runSound.IsPlaying then
			runSound:Play()
		end
	else
		PlayAnim:Stop()
		runSound:Stop()
	end
end

local leftControlDown = false

UIS.InputBegan:Connect(function(Key,IsTyping)
	if IsTyping then return end
	if Key.KeyCode == Enum.KeyCode.W then
		isRunning = true
		isAccelerating = true
		local Anim = script.SprintAnim
		PlayAnim = Character.Humanoid:LoadAnimation(Anim)
		ToggleAnimationAndSound(true)
		print('Running')
	elseif IsTyping.KeyCode == Enum.KeyCode.LeftControl then
		leftControlDown = true
	end
end)

UIS.InputEnded:Connect(function(Key,IsTyping)
	if IsTyping then return end
	if Key.KeyCode == Enum.KeyCode.W then
		Character.Humanoid.WalkSpeed = 13
		Character.Humanoid.JumpPower = 30
		ToggleAnimationAndSound(false)
		currentSpeed = 13
		currentAnimSpeed = 0.4
		isAccelerating = false
		isRunning = false
		print('Running stopped')
	elseif Key.KeyCode == Enum.KeyCode.LeftControl then
		leftControlDown = false
	end
end)

function ToggleRunningIfAirborne()
	if Character.Humanoid.FloorMaterial == Enum.Material.Air or leftControlDown then
		ToggleAnimationAndSound(false)
	elseif isRunning then
		ToggleAnimationAndSound(true)
	end
end

game:GetService('RunService').Heartbeat:Connect(function()
	ToggleRunningIfAirborne()
	if isAccelerating then
		increaseSpeed()
	end
end)
1 Like

Instead of key press detection, why not you just check the move direction of the humanoid in the heartbeat function? You could have a basic flag if you’re worried about it running multiple times

local heartbeatflag = false
game:GetService('RunService').Heartbeat:Connect(function()
	ToggleRunningIfAirborne()
	if isAccelerating then
		increaseSpeed()
	end
    if Character.Humanoid.MoveDirection ~= Vector3.new() then
        if heartbeatflag then return end
        heartbeatflag = true
        isRunning = true
		isAccelerating = true
		local Anim = script.SprintAnim
		PlayAnim = Character.Humanoid:LoadAnimation(Anim)
		ToggleAnimationAndSound(true)
		print('Running')
   else
      if not heartbeatflag then return end
      heartbeatflag = false
      Character.Humanoid.WalkSpeed = 13
		Character.Humanoid.JumpPower = 30
		ToggleAnimationAndSound(false)
		currentSpeed = 13
		currentAnimSpeed = 0.4
		isAccelerating = false
		isRunning = false
		print('Running stopped')
   end
end)
2 Likes

If you are having trouble with getting input from mobile, you can use ContextActionService.

It allows you to create an on-screen button for mobile players, which is also customizable. Read the documentation for more info.

Oh right, thank you i totally forgot that existed!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.