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

How do i make this a hold keydown script?

Asked by 7 years ago
Edited 7 years ago

what i am trying to do is when you hold m the Gui begins to increase in size. the problem is that instead of holding this starts to work when i click it, how can i make it into a hold script and if i let go it reverts back to original size and has to start over? nothing in output


if key == "m" and db == false then db = true mining = true while b < 100 do wait(.1) b = b+1 player.PlayerGui.ScreenGui.Frame.Hold.Completion.Size = player.PlayerGui.ScreenGui.Frame.Hold.Completion.Size + UDim2.new(0.01,0,0.0,0) end db = false end eventmouse.KeyUp:connect(function(key) if key == "m" and mining == true then mining = false player.PlayerGui.ScreenGui.Frame.Hold.Completion.Size = UDim2.new(.05,0,1,0) end end) end)

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

Alright, from your script it looks like you're using KeyDown event for the Mouse. I would suggest you rather use the UserInputService. The KeyDown event is sadly deprecated now even though it was easier to use. Anyhow, the UserInputService is much more efficient and prevents lag. So what you want to make it to where you want the Gui to expand when you're holding down the Key "M". I did my code using UserInputService and I am going to present to you the code I did and explain it with the commenting on each line. First of all, I placed this script under the Gui that I resized. I used a LocalScript of course, since I did it client sided. Without any further wait here is the script:

local player = game.Players.LocalPlayer -- Defined the variable player using the LocalPlayer method for local scripts.
local uis = game:GetService("UserInputService") -- Got the Service of UserInputService.
local gui = script.Parent -- Got the Gui that I am resizing.
local deb = true  -- A debounce that I set to true from the top.

function expand(inputObject, gp) -- A function that will expand the Gui. It uses the arguments of inputObject and gp which checks if you meant to click that key. So, if you're typing and you press that key it won't run this function.
    if inputObject.KeyCode == Enum.KeyCode.M and not gp and deb then  -- If Statement checking if the key is "M" and it checks if it's a Game Processed event. Also checks if the debounce is true.
    deb = false -- Sets debounce to false
    while deb == false do -- A conditional infinite loop that goes on forever, until debounce is not equal to false.
        wait() -- Wait for the loop or else it will crash.
    gui.Size = gui.Size + UDim2.new(0.001, 0, 0.001, 0) -- Adding 0.001 to the scaling
    end -- End for the while loop
end -- End for the if statement
end -- End for the function

function goback(inputObject, gp) -- Function to make the Gui go back to it's original size.
    if inputObject.KeyCode == Enum.KeyCode.M and not gp and deb ~= true then -- If statement to check if the KeyCode is "M" and to check if it's a game processed event. It also checks if the debounce doesn't equal true. 
    deb = true -- Sets the debounce to true so that the while loop stops.
    gui.Size = UDim2.new(0, 100, 0, 100) -- Sets the Gui to it's original size. You might want to change the size in there if you have a different size to set it back to.
    end -- End for the if statement.
end -- End for the function.


uis.InputBegan:connect(expand)
uis.InputEnded:connect(goback)

The above script, make sure it is placed under the Gui unless you defined the Gui some other way. Also, make sure it's a local script if it's gonna be placed on the Client side.

I hope this helped you and thank you for putting your precious time into reading my answer.

PEACE OUT!

~~ KingLoneCat

Ad

Answer this question