Function that starts under 2 condition

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I would like to be able to start a function if the player is physically touching the part and at the same time if he’s clicking a keyboard key, like “E”.

  2. What is the issue? Include screenshots / videos if possible!
    Idk how to write it

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Didn’t found anything

1 Like

use and

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

local Character = Players.LocalPlayer.Character

local function IsTouchingPart(Part)
	local OverlapParam = OverlapParams.new()
	OverlapParam.FilterType = Enum.RaycastFilterType.Whitelist
	OverlapParam.FilterDescendantsInstances = Character:GetDescendants()
	
	return #workspace:GetPartsInPart(Part, OverlapParam) > 0
end

if UserInputService:IsKeyDown(Enum.KeyCode.E) and IsTouchingPart(workspace.Part) then
	print("e and touching part")
end
3 Likes

Anyway to make this in a server script?

No. You can’t use UserInputService on the server. Use a local script.

1 Like

Oh ok ok. Thanks for letting me know.
@BirdieI90 Thanks for the solution

You can fire a server event when someone presses E and check the second condition on the server, and then do whatever you wanted to do there.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.