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

show gui ONLY when holding down E and dont show when not holding down?

i tried different solutions but all just had the same result of this basic script:

local frame = script.Parent.EmoteFrame

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
    if key:lower() == "e" or key:upper() == "E" then
       	frame:TweenPosition(UDim2.new(0.5,0,0.059,0),"Out","Quad",0.2,true)
	else
		frame:TweenPosition(UDim2.new(0.5,0,-0.15,0),"Out","Quad",0.2,true)
    end
end)

2 answers

✓ Accepted answer

Try using UserInputService.

https://developer.roblox.com/en-us/api-reference/class/UserInputService

You can do something like this.

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.KeyCode == Enum.KeyCode.E then
		--Code
	end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.KeyCode == Enum.KeyCode.E then
		--Code
	end
end)

You have to use UserInputService and its ImputBegan and InputEnded events. Mouse.KeyDown is deprecated.

local UserInputService = game:GetService("UserInputService")

local frame = script.Parent.EmoteFrame
frame.Visible = false

local player = game.Players.LocalPlayer

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.E and not gameProcessedEvent then
		frame.Position = UDim2.new(0.5,0,0.059,0)
		frame.Visible = true
	end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.E and not gameProcessedEvent then
		frame.Visible = false
	end
end)

The InputBegan event fires when a client makes an input (keyboard keys, mouseButton, etc.) Then there's an if statement checking if the key is "E" and the player isn't in chat. Finally, it makes the frame visible.

The InputEnded event fires when a client ends an input (keyboard keys, mouseButton, etc.) Then there's an if statement checking if the key is "E" and the player isn't in chat. Finally, it makes the frame invisible.

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