I’m trying to create a system where an NPC greets a player (using a normal speech bubble) when they’re within a certain distance of the NPC.
I would like the greeting to be in a standard text bubble but I am having trouble finding a tutorial that automatically greets players within a certain distance, without them having to use a ProximityPrompt first for the text to appear or the Dialogue system on Roblox Studio which requires you to press the speech bubble before the text appears.
Is there a way to do this, and if so, is there anyone who has a link to a tutorial or a model which does this? I have been unable to find either.
Heres my approach. First, make a part inside the NPC called ‘Range’, it should be invisible and have can collide set to false, and it should also be welded to the humanoidrootpart. Next, put this script somewhere into the NPC
local Rig = script.Parent
local Range = Rig:WaitForChild("Range")
local Chat = game:GetService("Chat")
local Inside = {}
while task.wait() do
local Inside_Local = {}
local op = OverlapParams.new()
op.FilterType = Enum.RaycastFilterType.Exclude
op.FilterDescendantsInstances = {Rig}
for _,v in pairs(workspace:GetPartBoundsInBox(Range.CFrame,Range.Size,op)) do
if v.Parent:IsA("Model") and v.Parent:FindFirstChild("Humanoid") then
table.insert(Inside_Local,v.Parent)
if not table.find(Inside,v.Parent) then
table.insert(Inside,v.Parent)
Chat:Chat(Rig.Head,"Hi!") -- if you want this to be local only, instead of porting the code to the client you could just fire a remote event instead
end
end
end
for k,v in pairs(Inside) do -- this loop removes players that are already inside
if not table.find(Inside_Local,v) then
table.remove(Inside,k)
end
end
end
This approach isnt perfect and actually it sucks but I hope its at least a good starting point. Also note that this only works on the server but if you want to make it local only, you can change Chat:Chat() to fire a remote event which will trigger a chat bubble to be made on the client.