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

Why wont my W key pressed to hide a GUI script work?

Im trying to make it so when you press W a GUI gets disabled but it says Expected ')' (to close '(' at line 2), got

i don't really understand scripting that well but i tried to fix it but couldn't

here is the script btw

local m = game.Players.LocalPlayer:GetMouse()
m.KeyDown:connect(function(k)
	k = k:lower()
	if k == "w" then
		script.Parent.Fist.Enabled = false
		end
	end

i tried adding ) to all around the second line but wouldn't help

(the title is weird because i had to make it "specific")

3 comments

0

Don't use keydown, it is deprecated (old code), instead use UserInputService

0

It's because you're trying to use a Roblox Utilized key with the deprecated KeyDown service. Roblox will prevent you from using keys 0-9, W, A, S, D, I, O, (I believe L for whatever reason) due to core scripts already using them. Use UserInputService InputBegan.

0

^

2 answers

the bracket is to close the Connect

local m = game.Players.LocalPlayer:GetMouse()
m.KeyDown:Connect(function(k)
    k = k:lower()
    if k == "w" then
        script.Parent.Fist.Enabled = false
        end
    end)--here

2 comments

0

Keydown and Keyup are deprecated, refrain from using them in future.

0

Yes i know im answering based on his syntax errors

I'm assuming you do not know this, but KeyDown and KeyUp are deprecated. This means it's old code which shouldn't be used, as it may be buggy or there are better options available.Read this article for more info. So, you may be asking, what do I use if they're deprecated? Simple, UserInputService. Here's an example of it:

game:GetService("UserInputService").InputBegan:connect(function(input) --When a key is pressed...
    if input.KeyCode == Enum.KeyCode.W then --Check if the key is W
        -- Do something when W is pressed!
    end
end)

Now, i strongly recommend double checking your code for deprecated code, for example, :connect is deprecated too, instead use :Connect , with a capital C. Right, now we've got that over with let's mash your code and the new working code together:

game:GetService("UserInputService").InputBegan:connect(function(input)pressed...
    if input.KeyCode == Enum.KeyCode.W then
        script.Parent.Fist.Enabled = false --Change the boolean to False.
    end
end)

For future reference:

Useful Links:

UserInputService, ContextActionService and getting user input.

Why you shouldn't use deprecated code

1 comment

0

Use connect, not Connect. Also, I recommend checking if the player is chatting or not. This way, the GUI won't close if the player is trying to chat.

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