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

How do Touch function and KeyDown work together?

I'm not sure if i'm doing correctly but I made lots of errors here, I would need help on how to fix this for my game.

script.Parent.Touched:connect(function()
	script.Parent.BillboardGui.Enabled = true
Mouse.KeyDown:connect(function(key)
 if (key == "e") then
	script.Parent.BillboardGui.TextBox.Text = "Here's help."
	wait(5)
	script.Parent.BillboardGui.TextBox.Text = "Press |E| for help"
end
end)
while
script.Parent.TouchEnded:connect(function()
	script.Parent.BillboardGui.Enabled = false
end
end)


I can't make it work somehow.

1 comment

0

Rn from line 3, I am hearing, If u click the mouse then if the mouse clicked is e then so and so, so exactly idk whats happening around line 3

1 answer

✓ Accepted answer

Getting user input from the mouse is deprecated, like I said for the millionth time. Instead, use UserInputService. For anyone saying that wait() is not deprecated, yes it is. It is crossed out.

script.Parent.Touched:Connect(function() --:connect is deprecated, use :Connect
	script.Parent.BillboardGui.Enabled = true
	game:GetService('UserInputService').InputBegan:Connect(function(key)
		if key.KeyCode == Enum.KeyCode.E then
	    	script.Parent.BillboardGui.TextBox.Text = "Here's help."
	   	Wait(5) --wait() is deprecated, use Wait()
	    	script.Parent.BillboardGui.TextBox.Text = "Press |E| for help"
	end)
end)

Also, do not use loops to listen for events. Just, just unnecessary.

script.Parent.TouchEnded:connect(function()
script.Parent.BillboardGui.Enabled = false
end

Also, do NOT use space to indent. Tab is quicker.

5 comments

0

first of all in your second code YOU indented wrong and :connect is not depreacted :Connect is..

0

:connect works fine so does :Connect..

0

Thanks for advice on UserInputService, however, this does not solve my problem yet.

0

Whoops. The reason I indented wrong is because I used TNTsquad's code as a template and he indented wrong and I didn't catch it. Also, what is still your problem?

0

Also, :connect is deprecated, that why it is crossed out. http://wiki.roblox.com/index.php?title=RBXScriptSignal#connect_2

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