Please note, this works some what. My problem is if 2 NPC’s are near each other, it will only show the talk for the first one you came close to. When you go to talk tho, you talk to the correct NPC (the one you are closest too)
It has to do with
local ActionClone = PlayerGui:FindFirstChild('TalkAction')
if ActionClone then return end
And I tried just doing if ActionClone exists then delete it, but that causes the UI to be constantly made and deleted like a million times a second.
local function RenderStepped()
local LastInput = UserInputService:GetLastInputType()
NPC = nil
local Character = Player.Character
if Character then
local HumanoidRootPart = Character:FindFirstChild('HumanoidRootPart')
if not HumanoidRootPart then return end
for _, v in pairs(NPCs:GetChildren()) do
local Distance = (v.PrimaryPart.CFrame.Position - HumanoidRootPart.CFrame.Position).magnitude
if not NPC or Distance <= NPC.Distance then
NPC = {
Current = v,
Distance = Distance
}
end
end
if not NPC then return end
Distance = NPC.Distance
if Distance <= ReachableDistance then
-- Show action
local DialogueClone = PlayerGui:FindFirstChild('Dialogue')
if DialogueClone then return end
local ActionClone = PlayerGui:FindFirstChild('TalkAction')
if ActionClone then return end
local ActionClone = Action:Clone()
ActionClone.Name = 'TalkAction'
if LastInput == Enum.UserInputType.Touch then
ActionClone.Control.Tap.Visible = true
ActionClone.Control.Key:Destroy()
ActionClone.Control.Activated:Connect(function()
Start()
end)
end
ActionClone.Adornee = NPC.Current
ActionClone.Parent = PlayerGui
local ActionTween = TweenService:Create(ActionClone.Control, TweenInfo.new(0.15), {Size = UDim2.new(1, 0, 1, 0)})
ActionTween:Play()
else
-- Too far, delete action
DeleteAction()
end
end
end
RunService.RenderStepped:Connect(RenderStepped)
Example of what I mean