I have a script but it only works for PC, and I want to be able to also do it on mobile, but they do not use keys and I'm pretty bad with UserInputService. Any advice?

--// Services
local repStr = game:GetService'ReplicatedStorage'
local UserInputService = game:GetService'UserInputService'

--// References
local plr = game.Players.LocalPlayer
repeat wait() until plr.Character
local char = plr.Character
local hum = char:WaitForChild'Humanoid'
local step = repStr:WaitForChild'step'

--// Variables
local timeElapsed
local lastStep = 0

KeyHeld = false
w = false
a = false
s = false
d = false

function onKeyPress(inputObject,gameProcessed)
    if inputObject.KeyCode == Enum.KeyCode.W then
		w = true
        KeyHeld = true
	elseif inputObject.KeyCode == Enum.KeyCode.A then
		a = true
		KeyHeld = true
	elseif inputObject.KeyCode == Enum.KeyCode.S then
		s = true
		KeyHeld = true
	elseif inputObject.KeyCode == Enum.KeyCode.D then
		d = true
		KeyHeld = true
    end
end

function onKeyRelease(inputObject,gameProcessed)
    if inputObject.KeyCode == Enum.KeyCode.W then
        w = false
		if a == false and s == false and d == false then
			KeyHeld = false
		end
elseif inputObject.KeyCode == Enum.KeyCode.A then
	a = false
	if w == false and s == false and d == false then
		KeyHeld = false
	end
		elseif inputObject.KeyCode == Enum.KeyCode.S then
			s = false
			if w == false and a == false and d == false then
				KeyHeld = false
			end
	elseif inputObject.KeyCode == Enum.KeyCode.D then
		d = false
		if w == false and a == false and s == false then
			KeyHeld = false
		end
    end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)
game:GetService("UserInputService").InputEnded:connect(onKeyRelease)

while wait(.13) do
	if KeyHeld ~= false then
		wait(.13)
			local elapsed = tick()-lastStep
			lastStep = tick()
			if elapsed >= .13 then
				step:FireServer()
			end
	end
end

3 comments

0

HumanoidStateType...? The whole script you wrote is useless

0

HumanoidStateType doesn't appear to be giving me consistent results.

0

For readability purposes you should NOT be using wait() as a condition.!