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

UserInputService, need urgento helpo~?

Asked by 9 years ago
01local player = game.Players.LocalPlayer
02local inventoryVis = player.PlayerGui.Inventory.Visible
03 
04UIS = game:GetService("UserInputService")
05 
06UIS.InputBegan(function(input, process)
07    if not process then
08        if input.KeyCode == Enum.KeyCode.G and inventoryVis == false then
09            --fetch inventory
10            inventoryVis = true
11        elseif input.KeyCode == Enum.KeyCode.G and inventoryVis == true then
12            --close inventory
13            inventoryVis = false
14        end
15    end
16end)

Trying to get it so that whenever the player presses G, an inventory GUI becomes visible, and turns it off if its already visible.

1 answer

Log in to vote
1
Answered by
Hero_ic 502 Moderation Voter
9 years ago
01local player = game.Players.LocalPlayer
02local inventoryVis = player.PlayerGui.Inventory.Visible
03 
04UIS = game:GetService("UserInputService")
05 
06UIS.InputBegan:connect(function(input, process)
07    if not process then
08        if input.KeyCode == Enum.KeyCode.G and inventoryVis == false then
09            --fetch inventory
10            inventoryVis = true
11        else then
12            --close inventory
13            inventoryVis = false
14        end
15    end
16end)

this should fix it all I did was remove the input.KeyCode == Enum.KeyCode.G and inventoryVis == true also you where missing "connect"

hope this helped. ~KIHeros

0
Thanks!! konichiwah1337 233 — 9y
0
@KiHeros 1) You shouldn't use variables to get properties, like you did with 'inventoryVis'. You will store the value and not the key (property) to it. So, if 'Inventory'-s Visible property does change, the variable will not. You should just store the 'Inventory' object itself. 2) At line 11, you wrote 'else then' which will error. LetThereBeCode 360 — 9y
0
PS. I know you have helped @konichiwah1337 further, but you should've updated the code, so other people can get help if they stumble upon similar problem. LetThereBeCode 360 — 9y
Ad

Answer this question