When I am holding down the ContextActionService button using the emulator, it does not work. This script is a local script located inside the character.
--//Services\\-- local contextActionService = game:GetService("ContextActionService") --//Camera\\-- local currentCamera = workspace.CurrentCamera --//Other\\-- local character = script.Parent local humanoid = character:WaitForChild("Humanoid") local statsFolder = character:WaitForChild("Stats") local statsDinosaur = statsFolder.Dinosaur local statsActions = statsFolder.Actions --//Script\\-- local function mobileSprint(inputState) if statsActions.CanSprint.Value then if inputState == Enum.UserInputState.Begin then humanoid.WalkSpeed = statsDinosaur.SprintSpeed.Value currentCamera.FieldOfView = 75 elseif inputState == Enum.UserInputState.End then humanoid.WalkSpeed = statsDinosaur.WalkSpeed.Value currentCamera.FieldOfView = 70 end end end local mobileSprintButton = contextActionService:BindAction("MobileSprintButton", mobileSprint, true, Enum.KeyCode.LeftShift) contextActionService:SetTitle("MobileSprintButton", "Sprint") contextActionService:SetPosition("MobileSprintButton", UDim2.new(0, 200, 0, 60)) contextActionService:GetButton("MobileSprintButton").Size = UDim2.new(0, 65, 0, 65)
I fixed the problem myself. Here is the final script:
--//Services\\-- local contextActionService = game:GetService("ContextActionService") --//Camera\\-- local currentCamera = workspace.CurrentCamera --//Other\\-- local character = script.Parent local humanoid = character:WaitForChild("Humanoid") local statsFolder = character:WaitForChild("Stats") local statsDinosaur = statsFolder.Dinosaur local statsActions = statsFolder.Actions --//Script\\-- local function Sprint(bindName, inputState) if inputState == Enum.UserInputState.Begin and bindName == "SprintBind" then currentCamera.FieldOfView = 75 Humanoid.WalkSpeed = statsFolder.SprintSpeed.Value elseif inputState == Enum.UserInputState.End and bindName == "SprintBind" then currentCamera.FieldOfView = 70 Humanoid.WalkSpeed = statsFolder.WalkSpeed.Value end end contextActionService:BindAction("SprintBind", Sprint, true, Enum.KeyCode.LeftShift)
It looks like the sprint function is missing some parameters. Functions bound via BindAction
are called with three parameters -- the name given by BindAction
, the UserInputState
, and an InputObject
.
If a function is called with more parameters than it actually has, the excess ones will be ignored. In this case, since the sprint function only has one parameter, it gets the name, but not the UserInputState
value or the InputObject
.