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

How would I Fix this KeyDown Event?

What is wrong with this Script. Is it because I'm using the GetMouse event or is it because of line 6?

open = script,Parent

function KeyDown(key)
	local key = key:lowerkey()
	if key == "e"  then
		game.StarterGui.sttngpg.plrstats.Visible = true
	end
end

open.Parent.plrstats:GetMouse().KeyDown:connect(KeyDown)

please Help!

2 comments

0

It's because of line 6; You are changing the GUI from 'StarterGui', not the actual Player's 'PlayerGui', also, 'lowerkey' is not an actual Method, consider changing to 'key:lower()'?

0

How would I write that?

2 answers

✓ Accepted answer

Your problems are lines 4, and 6: On line 4, lowerkey is not a valid Method, consider changing to key:lower()?, and, on line 6, you are only changing the StarterGui's GUI version, lastly, there is another two problems I am noticing; Lines 6 and the last Line; You do not need to use that much code, the more problems with two codes are; What if they do not exist? Then what? What can I do to prevent this?, this can also be fixed, however, it will require using the WaitForChild Method. Now that I have pointed out all these problems, lets fix your code;

local open = script.Parent
local plrstats = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("plrstats") --This looks much cleaner, doesn't it? ;)
local plrstats2= game.Players.LocalPlayer:GetMouse() --This looks much cleaner, doesn't it? ;)


function KeyDown(key)
    local key = key:lower()
    if key == "e"  then
        plrstats.Visible = true
    end
end

plrstats2.KeyDown:connect(KeyDown)

Ah, now doesn't your code looks allot better, and cleaner? :) Hope this helped!

10 comments

0

doesn't work. Have you tested it?

0

Are you using a 'Server-Sided' script [Normal Script], or a 'Client-Sided' script [Local Script]?

0

Client-Sided

0

Huh, then, I must not know how the code is set up. Is your issue that 'KeyDown' does not work?

0

yes

0

Hmm, it may either that 'plrstats' is not the actual name, or.. Wait.. I think I might know the issue! Is 'plrstats' a 'ScreenGui' or 'TextLabel' type instance?

0

Its a Frame

0

That's your problem; Your not using a 'ScreenGui' type instance, that is REQUIRED for GUIs to show up. Simply create a new 'ScreenGui', then put your Frame in it. Fixed.

0

Thanks man! Your the best!

0

No problem! :D Glad to help! :)

First: It's not:

local key = key:lowerkey()

Change it for:

local key = key:lower()

And continue making the script <3

2 comments

0

doesn't work

0

Remember change line 6: Change StarterGui for PlayerGui.

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