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

My userinputservice script isnt working?

Asked by 7 years ago
Edited 7 years ago
    local enabled = true

    local function onInputBEGAN(inputObject, gameprocessed)
        if inputObject.KeyCode == Enum.KeyCode.E then
            print("pressed")
            enabled = false
        end 
    end

    local function onInputENDED(inputObject, gameprocessed)
        if inputObject.KeyCode == Enum.KeyCode.E then
            print("released")
            enabled = true
        end 
    end 

    if enabled == true then
        game:GetService("UserInputService").InputBegan:connect(onInputBEGAN)
    elseif enabled == false then
        game:GetService("UserInputService").InputEnded:connect(onInputENDED)
    end 

The script should print 'pressed' when the 'E' key is pressed and should print 'released' when it's released, but it's not working. There are no errors printed in the output either.

What's wrong with my script? Is there an easier, more efficient way to do this?

Please help.

0
Where is 'Character:FindFirstChild("Kagune")' part? User#5423 17 — 7y
0
Sorry forgot to remove that, it's not needed. SHDrivingMeNuts 299 — 7y

1 answer

Log in to vote
2
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
7 years ago
Edited 3 years ago

Problem

It's because of the if statement. The script thinks it has done its job once it completes that if statement and will just need to listen to any events necessary. Since enabled is true at the beginning of the script and when the if statement is run, the only event the script has to listen to is InputBegan.


Solution

For your situation, just make a temporary event. A temporary event means it can get disconnected when you need to get rid of it. Since temporary events are somewhat buggy, you will need to define the variable before defining the temporary event. We will then need to set the new OnInputENDED variable equal to the event and anonymous function.

We will take the anonymous function and execute any code we need to execute. Once you are all done with that, we will need to :disconnect the temporary event at the end.


Final Script

local enabled = true

local function onInputBEGAN(inputObject, gameprocessed)
    if inputObject.KeyCode == Enum.KeyCode.E then
        print("pressed")
        enabled = false

        local onInputENDED --We just need to let the script know, hey this is real. May be nothing but it's real.

        onInputENDED = game:GetService('UserInputService').InputEnded:connect(function(inputObject) --We're making an anonymous function, this will be our event we're connected to.
            if inputObject.KeyCode == Enum.KeyCode.E then
                print('released')
                enabled = false

                onInputENDED:disconnect() --Here, the event gets tossed out. This way you're not seeing double of prints and the code isn't doing double.
            end
        end)



    end 
end

game:GetService("UserInputService").InputBegan:connect(onInputBEGAN)

Hopefully this answered your question. If it did do not forget to hit the accept answer button. If you have any questions, feel free to comment below. If you were expecting the print for press to continually print, you should have explained that in your post as it is a different technique.
0
Thanks, Mega-sama. SHDrivingMeNuts 299 — 7y
Ad

Answer this question