I’ve made a system where you can press C to crouch and then press it again to uncrouch, and I want to make this crouch system mobile supported, but I don’t really know how. I’m using ContextActionService and binding the button to C, but it doesn’t seem to work. Am I missing something?
Code:
local CAS = game:GetService("ContextActionService")
local function crouch()
print("Crouched")
end
local function ProcessAction(ActionName,InputState,InputObject)
if InputState == Enum.UserInputState.Begin then
if ActionName == "Crouch" then
crouch()
end
end
end
CAS:BindAction("Crouch", ProcessAction, true, Enum.KeyCode.C, Enum.KeyCode.ButtonY)
CAS:SetTitle("Crouch", "Crouch")
CAS:SetPosition("Crouch", UDim2.new({0.812, 0},{0.407, 0}))
Thanks in advance!
EDIT: Having the localscript enabled breaks the crouching system on PC also, and I don’t know why.
I tested your code and works fine for both, pc and “mobile” (I just tested the mobile mode on studio) Whats the problem you are experiencing with the code?
I have two scripts, the PC crouch (C to crouch), and the Mobile crouch, which is a button that should emulate C getting pressed. The issue is if the Mobile script is enabled the PC script doesn’t work, and I’ve tried importing the PC script and just making it that one script but that doesn’t seem to work either.
You dont need 2 scripts for this. By using this script you are showing, you are using CAS, and its binded to the Key C of a pc user, and creates a button if the player joins using a mobile. You dont need the “PC crouch” script. Just this one.
You are calling the function “crouch” when a pc user, press C and when a mobile user press the button. Thats all
Realized that, but the issue is that when I try to port the original script into the Function it doesn’t work at all, since the script has always been designed to Fit PC
If the script uses remote events and your able to tell if the player is mobile or not. A GUI could appear that can trigger the remote event your looking for…
It’s very clunky and not very neat , but it works perfectly fine and has no issues for PC.
Code:
local UIS = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = script:WaitForChild("anim")
local AnimTrack = Humanoid:LoadAnimation(Animation)
local Animation2 = script:WaitForChild("walk")
local AnimTrack2 = Humanoid:LoadAnimation(Animation2)
local isCrouched = false
local isWalking = false
local CC = game:GetService("ReplicatedStorage"):WaitForChild("CC")
local CR = game:GetService("ReplicatedStorage"):WaitForChild("CR")
UIS.InputBegan:Connect(function(key, gameProcessedEvent)
if not gameProcessedEvent then
if key.KeyCode == Enum.KeyCode.C then
if not isCrouched then
CC.Value = true
CR.Value = false
isCrouched = true
Humanoid.WalkSpeed = 10
AnimTrack:Play()
else
if not isWalking then
CC.Value = false
CR.Value = true
Humanoid.WalkSpeed = 16
isCrouched = false
AnimTrack:Stop()
end
end
end
end
end)
Humanoid.Running:connect(function(speed)
if speed > 0 then
if not isWalking then
if isCrouched then
isWalking = true
AnimTrack:Stop()
AnimTrack2:Play()
end
end
else
if isCrouched then
if isWalking then
isWalking = false
AnimTrack2:Stop()
AnimTrack:Play()
end
end
end
end)
Like this: (Obviously make sure all the references you showed in the second script are available for this one)
local function ProcessAction(ActionName,InputState,InputObject)
if InputState == Enum.UserInputState.Begin then
if ActionName == "Crouch" then
if not isCrouched then
CC.Value = true
CR.Value = false
isCrouched = true
Humanoid.WalkSpeed = 10
AnimTrack:Play()
else
if not isWalking then
CC.Value = false
CR.Value = true
Humanoid.WalkSpeed = 16
isCrouched = false
AnimTrack:Stop()
end
end
end
end
end
CAS:BindAction("Crouch", ProcessAction, true, Enum.KeyCode.C, Enum.KeyCode.ButtonY)
CAS:SetTitle("Crouch", "Crouch")
CAS:SetPosition("Crouch", UDim2.new({0.812, 0},{0.407, 0}))
So, with only one script using CAS, the pc player and the mobile player will be able to trigger the crouch function. The PC player using the keyboad C key, and the mobile one, will get a button on screen, both will trigger the same function in the CAS