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..
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
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.