Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is there a way to check if a player is stepping on a certain object and presses a certain key?

In my game, I'm trying to make it when a player is stepping on a certain part, and presses the f key, an animation will play. Is there a way to do that easily, without having to make multiple scripts? Thank you, if you can answer.

1 answer

You'll need UserInputService for this. It's actually pretty simple to detect a key being pressed.

You'll need the part you want to check for (the one they're stepping on) and an animation to use when they have pressed the key.

local UserInputService = game:GetService('UserInputService')
local Part = game.Workspace.Part

local LocalPlayer = game.Players.LocalPlayer
local PlayerCharacter
local Humanoid

local Animation = script.Animation --Put animation inside script

repeat wait() until LocalPlayer.Character
PlayerCharacter = LocalPlayer.Character
repeat wait() until PlayerCharacter:FindFirstChild('Humanoid')
Humanoid = PlayerCharacter['Humanoid']

local function CheckDistance()
	if (PlayerCharacter['Torso'].Position - Part.Position).magnitude < 5 then
		local Animate = Humanoid:LoadAnimation(Animation)
		
		if Animate then
			Animate:Play()
		end
	end
end

UserInputService.InputBegan:Connect(function(Input, GameProcessed)
	if Input.UserInputType == Enum.UserInputType.Keyboard then
		if Input.KeyCode == Enum.KeyCode.F then
			print('Pressing F')
			CheckDistance()
		end
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.