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

KeyDown to Pick up A Gear?

Asked by 6 years ago

So I know how to use KeyDown, but i want this to happen:say theres an apple on the ground, I want the player to press "f" to pick it up. So I can do a KeyDown script to show a ScreenGUi.

TY for our help..

0
Use UserInputService, KeyDown is deprecated. http://wiki.roblox.com/index.php?title=API:Class/UserInputService RockerCaleb1234 282 — 6y

2 answers

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

First of all you are going to have to make sure the apple is close enough. To do this you have to calculate the distance between the apple and the humanoid. Use the following function =

h = humanoid.Position a = apple.Position

distance = math.sqrt(((h.X - a.X)(h.X - a.X))+((h.Z - a.Z)(h.Z - a.Z)))

Then, make sure that the apple is within range.

range = 5

Put the script below into a local script, then put it into starterGui. Put the gui you want to pop up in the starterGui aswell and disable the gui. When the f key is pressed and the player is in range, the gui will pop up!

player = game.Players.LocalPlayer
mouse = player:GetMouse()
character = game.Workspace:WaitForChild(player.Name)
humanoid = character.Humanoid
gui = player.PlayerGui.AppleGui                 --find the gui you want to pop up
apple = game.Workspace.Apple                --find the apple, this may not be right
range = 5                                       --you can change this
mouse.KeyDown:connect(function(key)
    if key == "f" then
        h = humanoid.Position               
        a = apple.Position
        distance = math.sqrt(((h.X - a.X)*(h.X - a.X))+((h.Z - a.Z)*(h.Z - a.Z)))
        if distance < range then
            gui.Enabled = true
        end
    end
end
Ad
Log in to vote
0
Answered by 6 years ago

Keydown is now a deprecated function meaning it will not work in game.

use UserInputService like this:

game:GetService('UserInputService').InputBegan:connect(function(input, process) -- basically if the player does anything w/ mouse, keyboard or other
    if input.InputType = Enum.InputType.Keyboard then  -- check if it was the keyboard being used
        if input.KeyCode = Enum.KeyCode.F then -- make sure the player pressed F
            -- do ur apple stuff here
        end
    end
end)

basically UserInputService.InputBegan means anytime the person has begun to press the mouse or a keyboard button or other it senses it.

0
open that up on notepad to copy properly then do ur stuff. MrDefaultMan 113 — 6y
0
I would say put the gui in lighting then clone it into plr.PlayerGui once F is pressed MrDefaultMan 113 — 6y

Answer this question