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.
Still have questions? Join our Discord server and get real time help.
0

# 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

1

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)

0

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.