Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

[SOLVED] Why is UserInputState not working? No errors in the output.

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)

4 comments

0

I've never used ContextActionService, but maybe the error is the fourth argument of: local mobileSprintButton = contextActionService:BindAction("MobileSprintButton", mobileSprint, true, Enum.KeyCode.LeftShift)

0

It has to do something with the UserInputState because before the "if statsActions.CanSprint.Value then" I added a print("test") and it printed "test" in the output.

0

Add a print(inputState) before the "if statsActions.CanSprint.Value then" and see what happens

0

Sorry for the 5hr late response, but in the output, it prints "MobileSprintButton."

2 answers

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.

2 comments

0

I have no idea why this answer is accepted, but I will give this a try and if it works, then I'll leave this answer accepted.

0

It doesn't seem to work, I might have did it wrong? The parameters I have are (bindName, inputObject, inputState).

This discussion is preserved as it was. New answers, comments, and votes are closed.