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 4 years ago

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

Log in to vote
0
Answered by 4 years ago

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)
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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.

Answer this question