New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Why dose this Key function Not work as intended?

Asked by 8 days ago

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

Log in to vote
1
Answered by 8 days ago

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)
Ad
Log in to vote
0
Answered by 8 days ago

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.

Answer this question