Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why dose this Key function Not work as intended?

So I am making a 3rd Person shooter game, and I am making a button that is activated with the key "K" so when you press "K" a Gui frame pops so I tried making that in a local script inside StarterGui here's the script

game:GetService("UserInputService").InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.K then
        game.StarterGui.ShopGui.Frame.Visible = true-- Turns on frame
  end
end)

I can't figure why it won't work

2 answers

The StarterGui is basically just a storage place for all the GUIs to be replicated to the client when they join the game and respawn. You want to reference the PlayerGui.

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

game:GetService("UserInputService").InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.K then
        LocalPlayer.PlayerGui.ShopGui.Frame.Visible = true-- Turns on frame
  end
end)

Make sure this is in a local script, as that is how you use the Input Service.

local player = game.Players.LocalPlayer

game:GetService("UserInputService").InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.K then
        player.PlayerGui.ShopGui.Frame.Visible = true-- Turns on frame
  end
end)

You shouldn't address the PlayerGui, as anything in that gets cloned into the player whenever they join. You always change GUIs in the player's PlayerGui.

This discussion is preserved as it was. New answers, comments, and votes are closed.