#Checking if a player is touching any parts and pressing f

2 messages · Page 1 of 1 (latest)

past epoch
#

``lua local Players = game:GetService("Players")

function checkTouchingPart(character)
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
local touchingParts = humanoidRootPart:GetTouchingParts()
for _, part in ipairs(touchingParts) do
-- Return true if the character is touching any part
return true
end
end
return false
end

function onInputBegan(input, gameProcessedEvent)
if input.KeyCode == Enum.KeyCode.F then
local character = Players.LocalPlayer.Character
if character and checkTouchingPart(character) then
-- Do something when player is touching a part and presses F
print("Player is touching a part and pressed F")
end
end
end

-- Connect the input event
local userInputService = game:GetService("UserInputService")
userInputService.InputBegan:Connect(onInputBegan)
``

#

Checking if a player is touching any parts and pressing f