Give item in hand to nearby player

hello, I want to make it so the item that is currently in a players hand (a drink) can be given to a nearby player by using a proximity prompt. (a waiter will bring the drink to player then will Hold E to give the drink to customer)
the only script i can find is for a click to give item. but i would like it to be a Hold E to give item to customer.

Many thanks!

You can use the .Triggered event of the prompt then looping through each player and finding the closest distance

Could you not loop through all players or something and check the difference between the item and the player?

You could use a proximity prompt to detect when the E button is clicked.

You could try to loop through all players in-game, and see which one’s HumanoidRootPart’s distance between him and you is the smallest, and give it to him.

this is the script i have. this it to click to give item. how to i make this code a proximity promt?

local tool = script.Parent

local canGive

local function onTouch(otherPart)

local humanoid = otherPart.Parent:FindFirstChild("Humanoid")

if humanoid then

    if humanoid.Parent ~= tool.Parent and canGive then 

        canGive = false

        tool.Parent = humanoid.Parent

    end

end

end

local function slash()

local str = Instance.new("StringValue")

str.Name = "toolanim"

str.Value = "Slash" 

str.Parent = tool

canGive = true

wait(.5)

canGive = false

end

tool.Handle.Touched:Connect(onTouch)

tool.Activated:Connect(slash)

this is the script i have. this it to click to give item. how to i make this code a proximity promt?

local tool = script.Parent

local canGive

local function onTouch(otherPart)

local humanoid = otherPart.Parent:FindFirstChild("Humanoid")

if humanoid then

    if humanoid.Parent ~= tool.Parent and canGive then 

        canGive = false

        tool.Parent = humanoid.Parent

    end

end

end

local function slash()

local str = Instance.new("StringValue")

str.Name = "toolanim"

str.Value = "Slash" 

str.Parent = tool

canGive = true

wait(.5)

canGive = false

end

tool.Handle.Touched:Connect(onTouch)

tool.Activated:Connect(slash)

this is the script i have. this it to click to give item. how to i make this code a proximity promt???

local tool = script.Parent

local canGive

local function onTouch(otherPart)

local humanoid = otherPart.Parent:FindFirstChild("Humanoid")

if humanoid then

    if humanoid.Parent ~= tool.Parent and canGive then 

        canGive = false

        tool.Parent = humanoid.Parent

    end

end

end

local function slash()

local str = Instance.new("StringValue")

str.Name = "toolanim"

str.Value = "Slash" 

str.Parent = tool

canGive = true

wait(.5)

canGive = false

end

tool.Handle.Touched:Connect(onTouch)

tool.Activated:Connect(slash)

You can use the .Triggered event of the prompt then looping through each player and finding the closest distance.

local prompt = --path to prompt

prompt.Triggered:Connect(function(plr)
   local char = plr.Character
   local root = char and char:FindFirstChild("HumanoidRootPart")
   if not root then return end


   local ClosestDist
   local Closest

   for _,v in pairs(game:GetService("Players"):GetPlayers()) do
       if v == plr then continue end

       local OtherChar = v.Character
       local HumanoidRootPart = OtherChar and OtherChar:FindFirstChild("HumanoidRootPart")

       if HumanoidRootPart then
          local dist = (HumanoidRootPart.Position-root.Position).Magnitude

          if not ClosestDist or dist < ClosestDist then
             ClosestDist = dist
             Closest = v
          end

       end
   end

   if Closest then
       --Do magic with Closest plr
   end
end)
1 Like

How do i attach the prompt to the player

You could just parent the prompt to one of the player’s limbs (HumanoidRootPart should work)

Where would the proximity prompt be located in my workspace etc or would it be an Instance.new?

I have to go now but you could create it with instance.new and set the properties then parent it or you could create it already and store it in maybe replicated storage then clone that each time