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