Matthew
Matthew

Reputation: 1

UserInputService is not working on roblox studio

I would like to know what I am doing wrong.

I just followed the documentation to make pressing the "E" key show a "print" on the console and say "Pressed" but it hasn't worked, what am I doing wrong?

-- input
local UserInputService = game:GetService("UserInputService")
--input

-- rolehandle
local function handletouched()
    handle.Touched:Connect(function(fas)
        wait(3)
        role.Value = "-"
        if game:GetService("UserInputService").InputBegan == Enum.KeyCode.E then
            print("pressed")
        end
    end)
end

Upvotes: 0

Views: 1035

Answers (1)

Piglet
Piglet

Reputation: 28974

Your code doesn't make too much sense. Not sure which documentation you followed. The Roblox documentation and its examples are pretty clear about what to do.

You define a function handletouched that is never called.

In that function you compare an event object vs a number, which of course are never equal.

Please read https://developer.roblox.com/en-us/api-reference/event/UserInputService/InputBegan

and

https://developer.roblox.com/en-us/api-reference/event/BasePart/Touched

You have an event that is fired by the game and you have to provide an event handler function. You implement that function and then connect it to the event. So whenever the event is fired, this function will be called.

You should read more carefully and first try to understand the examples given in the documentation befor you implement your own code.

Make sure you understand this:

https://developer.roblox.com/en-us/articles/events

Upvotes: 1

Related Questions