Using a mobile run button, how could i make it send leftshift and just use my other run script?

ok, i’ve been working on a mobile run button for my game and it just seems to need it’s own serverscript to function, although i was hoping that there was a way that i could use this mobile run button to just make it fire as an left-shift input to the client/server, instead of having to create a new run script.

is there any way i could do this? here is my run button script as of now:

-- I followed a tutorial as I had no idea how to go about doing this.

local cas = game:GetService("ContextActionService")
local run = Enum.KeyCode.LeftShift
local rs = game:GetService("ReplicatedStorage")
local re = rs:WaitForChild("runre")
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local rootpart = char:WaitForChild("HumanoidRootPart")

local function handleContext(name, state, input)
	if state == Enum.UserInputState.Begin then
		re:FireServer(24)
	else
		re:FireServer(16)
	end
end

cas:BindAction("Sprint", handleContext, true, run)
cas:SetPosition("Sprint", UDim2.new(.2,0,.5,0))
cas:SetTitle("Sprint", "Sprint")
cas:GetButton("Sprint").Size = UDim2.new(.3,0,.4,0)

i put this localscript in starterplayerscripts, if that helps.

thank you for reading!

2 Likes
local input = game:GetService('UserInputService')

input.InputBegan:Connect(function(input)
	input.KeyCode = Enum.KeyCode.W
	print(input.KeyCode)
end)

I am a little confused by what you were asking in your original post, but this is how I interpreted it.

Once you obtain the Input, you can just set your input variable to the desired Enum KeyCode item.

So for the mobile Input types, you can compare them before you do the checks you would do on PC, and then set them to the PC equivalent keycodes.

(Above is an example of what I mean, regardless of what my input is, it will print “W”)

2 Likes

It does not need a server script to work, if you have the run script inside a function you can put

cas:BindAction("Sprint", handleContext, true, run)

Inside your running script and replace handleContext with your run Function

1 Like

I don’t know, you might be able to do something with while true do?