Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 5 years ago

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

01local frame = script.Parent.EmoteFrame
02 
03local player = game.Players.LocalPlayer
04local mouse = player:GetMouse()
05 
06mouse.KeyDown:connect(function(key)
07    if key:lower() == "e" or key:upper() == "E" then
08        frame:TweenPosition(UDim2.new(0.5,0,0.059,0),"Out","Quad",0.2,true)
09    else
10        frame:TweenPosition(UDim2.new(0.5,0,-0.15,0),"Out","Quad",0.2,true)
11    end
12end)

2 answers

Log in to vote
0
Answered by 5 years ago

Try using UserInputService.

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

You can do something like this.

01local UserInputService = game:GetService("UserInputService")
02 
03UserInputService.InputBegan:Connect(function(input, gameProcessed)
04    if gameProcessed then return end
05    if input.KeyCode == Enum.KeyCode.E then
06        --Code
07    end
08end)
09 
10UserInputService.InputEnded:Connect(function(input, gameProcessed)
11    if gameProcessed then return end
12    if input.KeyCode == Enum.KeyCode.E then
13        --Code
14    end
15end)
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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

01local UserInputService = game:GetService("UserInputService")
02 
03local frame = script.Parent.EmoteFrame
04frame.Visible = false
05 
06local player = game.Players.LocalPlayer
07 
08UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
09    if input.KeyCode == Enum.KeyCode.E and not gameProcessedEvent then
10        frame.Position = UDim2.new(0.5,0,0.059,0)
11        frame.Visible = true
12    end
13end)
14 
15UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
16    if input.KeyCode == Enum.KeyCode.E and not gameProcessedEvent then
17        frame.Visible = false
18    end
19end)

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.

Answer this question