Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by
8_zn 21
5 years ago
Edited 5 years ago

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)
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) GenuineWaffles 35 — 5y
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. 8_zn 21 — 5y
0
Add a print(inputState) before the "if statsActions.CanSprint.Value then" and see what happens BrockRocksYourSocks 48 — 5y
0
Sorry for the 5hr late response, but in the output, it prints "MobileSprintButton." 8_zn 21 — 5y

2 answers

Log in to vote
1
Answered by
8_zn 21
5 years ago

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)
Ad
Log in to vote
0
Answered by 5 years ago

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.

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. 8_zn 21 — 5y
0
It doesn't seem to work, I might have did it wrong? The parameters I have are (bindName, inputObject, inputState). 8_zn 21 — 5y

Answer this question