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

Can someone breifly show me what a InputBegan event looks like?

Asked by 8 years ago

Thanks in advanced for help

1 answer

Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

UserInputService only works in LocalScripts. The wiki has some examples, too. Check out the wiki for InputObject to see all available types of input (you can also check for things like mouse clicks/scrolls).

local p = game.Players.LocalPlayer
game:service("UserInputService").InputBegan:connect(function(inputObject,gameProcessed)
    if gp == false and inputObject.KeyCode == Enum.KeyCode.F then -- if the key is F and the player isn't typing
        print(p.Name.." pressed F!")
    end
end)

From seeing an earlier question of yours, I'll make some code for you in context:

This is a LocalScript.

local gui = script:WaitForChild("TabGui") -- the gui
local uis = game:service("UserInputService")
local p = game.Players.LocalPlayer
local curGui -- for later

function guiToggle(enabled) -- function for toggling gui
    if enabled == true and not curGui then -- if the gui is closed and the script requests for it to be open
        curGui = gui:Clone() -- clones gui
        curGui.Parent = p.PlayerGui -- parents it
    elseif enabled == false and curGui then -- if the gui is open and the script requests for it to be closed
        curGui:Destroy() -- destroys gui
    end
end

uis.InputBegan:connect(function(key,gp)
    if gp == false and key.KeyCode == Enum.KeyCode.Tab then -- if the gui toggle is eligible to run
        if not curGui then -- if the gui is closed
            guiToggle(true) -- invokes the function, requesting to be opened
        else
            guiToggle(false) -- invokes the function, requesting to be closed
        end
    end
end)

Hope I helped!

~TDP

Ad

Answer this question