Need help with crouching for mobile users

I need help with adding crouching for Mobile user’s, how to add a gui script to the crouching script? her is the script, it only works for Computer I appreciate your help!

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Animate 
local Humanoid = player.Character:FindFirstChild('Humanoid')

mouse.KeyDown:Connect(function(Key) 
 if Key == "l" then
  local Animation = Instance.new("Animation", player.Character)
  Animation.AnimationId = --My id
  Animate = Humanoid:LoadAnimation(Animation)
  Animate:Play()
 end  
end)

mouse.KeyUp:Connect(function(Key)
 if Key == "l" then
  Animate:Stop()
 end
end)
2 Likes

you can create a special gui button for mobile players to make them crouch. heres a webpage on the DevHub on creating mobile buttons if you dont know how:
https://developer.roblox.com/en-us/articles/ContextActionService-Creating-Mobile-Buttons

2 Likes

So in this script where would i put the crouching code?

1. local ContextActionService = game:GetService("ContextActionService")
 * local function interactNPC(actionName, inputState, inputObject)
2. if inputState == Enum.UserInputState.Begin then
3. print(actionName, inputObject)
4. end
5. end
 * -- Bind action to function
6. ContextActionService:BindAction("InteractNPC", interactNPC, true, Enum.KeyCode.T, Enum.KeyCode.ButtonR1)

inside the if inputState == Enum.UserInputState.Begin then.
also, on the last row,

ContextActionService:BindAction("InteractNPC", interactNPC, true, Enum.KeyCode.T, Enum.KeyCode.ButtonR1)

you can customize the inputs after true,.
since your scipt is using the number key 1 to activate, you change EnumKeyCode.T to EnumKeyCode.1.

Enum.KeyCode.ButtonR1 is a button assigned to a controller, so you can remove it if your just targeting for your input to work for mobile, but i’d keep it just in case.

but the KeyUp event on mobile wont work, so you could just change it to have a toggle inside it. like this,

local toggle = false --creates a varible with a false value
    local ContextActionService = game:GetService("ContextActionService")
     
    local function interactNPC(actionName, inputState, inputObject)
    	if inputState == Enum.UserInputState.Begin then
if toggle == false then --if varible toggle = false, it will run
                toggle = true -- sets the value inside varible toggle to true
    		print(actionName, inputObject)--replace this with whatever you want to be run here
else  --if varible toggle isnt = false, it does this
toggle = false -- sets the value inside varible toggle to false
    	end
    end
    ContextActionService:BindAction("InteractNPC", interactNPC, true, Enum.KeyCode.1, Enum.KeyCode.ButtonR1) 

note: if you read the link i gave you in an earlier post, you’ll find a table that gives the usage of ContextActionService:BindAction’s parameters. if you dont know the use of the parameters of it, i suggest reading more carefully. you can find the table near the top of the page.

When I type that code into a local script it doesn’t work, what went wrong?

did it give an error message when it was run? if so, can you reply with the error message?

Not related to your issues, but just a quick advice - you should consider using either user input service or context action service even for non mobile users, mouse.KeyDown has been deprecated a while ago.

Where inputState and inputObject is do I put something there? cause I just wrote that out with the of the code.

just editted my post above, gave more detail to the code in the post at the bottom. i also put a note at the bottom too, which i recommend you read to answer your previous reply.

Ok, I’m going to edit the code, but if it doesn’t work Would this code work?

local ContextActionService = 
game:Getservice("ContextActionService")
function onButtonPress( )

If so could I add my crouch animation to it?

P.S. the KeyCode.1 1 is attually a L

before we go further into this, are you writing the code in a Script or LocalScript? cause events that fire when a player presses a curtain key/button only work in LocalScripts and ModuleScripts.

Yes I’m writing this in a LocalScript in PlayerGui

what you can do if you dont have the knowledge of binding keybinds together(or if your too lazy to find out how to), is you can make guibuttons in a ScreenGui, then add a LocalScipt in it that if the players choice of gameplay is on mobile, it shows the guibutton and if not, it hides it

trying to find the event that checks what device the player is playing on, so i can give out a piece of code to give you an example.


Note: this will work, but it is flawed if the players choice of gameplay is mobile, but the player uses an external device to play(like a bluetooth keyboard, or a bluetooth controller) and switches to that externall device while playing.

Ok, thanks a lot for your help, I wouldn’t mind if the gui shows even if on a computer. You shouldn’t give me a code, that’s is laziness so i wouldn’t recommend you write the code. I appreciate you helping me, it let’s me understand scripting more.

I would recommend using ContextActionService to bind the action. However, the mobile buttons created with ContextActionService have limited amount of configuration, and what I do is create the mobile button manually, using Image/Text Buttons(using UserInputService:GetLastInputType())

Here it is, one part is done, now the next part I still don’t know how to connect my crouch animation to it.

local animate 

function onKeyPress (actionName, 
userInputState, inputObject)
        if userInputState == 
Enum.UserInputState.Begin then
   print(" l pressed")
       end
end  
game.ContextActionService:BindAction("keyPressAnyName", onKeyPress, true, Enum.KeyCode.l)


As you can see it does not crouch, how do I achieve that?

You will need to learn about animations, use AnimationTrack:Play() inside the function used. For more information on using animations in games, look here
edit: you already know about animations, the function you have should use AnimationTrack:Play() but load the animation track outside the function