NPC Dialogue - Getting Different Dialogue when NPC Moves to Different Position

function positionDialogue()
	local currentNPCRoot = ebutton.Parent
	local currentPosition = currentNPCRoot
	
	print(currentPosition.Position)
	if currentPosition.Position == Vector3.new(-15.55, 2.7, 44.57) then
		dialogue = require(script:WaitForChild("DialogueOne"))
			
	end
end

Still nothing

Summary

Ok, what’s the ebutton and the ebutton's parent

The ebutton is in replicated storage and can be seen in the image below.

Summary

The ebutton is placed into the NPCs HumanoidRootPart through the script.

Summary

image

So the ebutton's parent is the RootPart

1 Like

And you’re near the npc right?

This is output now, I am not sure how it changed?

Summary

Edit: Yes. The player has to be within so many studs for the ebutton to appear

1 Like

DialougeOne is a valid child of the script right?

Edit:
try changing

currentPosition.Position == Vector3.new(-15.55, 2.7, 44.57)

to

currentPosition.Position == Vector3.new(-15.54, 2.7, 44.57)

Yes, you can see it in StarterPlayerScripts here

Summary

1 Like

I wouldn’t recommend getting the dialogue from checking the position, as it could be unreliable and change. In this instance, what I’d do is I’d give the NPC a value. Each value represents a point. You change the value when you change the position. Then, instead of checking the position, you check the value.

It appears that it can’t find the dialogue variable.
If you add the dialogue variable at the top, it should scope and find it instead of erroring it.

local player = game.Players.LocalPlayer
local npcs = workspace.NPCs:GetChildren()
local ebutton = game.ReplicatedStorage.EButton
local uis = game:GetService("UserInputService")
local dialogueUI = player.PlayerGui:WaitForChild("DialogueUI")
local bg = dialogueUI.Background 
local currentPage = 1
local currentNPC = ""
local dialogue
2 Likes

Okay I tried changing to

currentPosition.Position == Vector3.new(-15.54, 2.7, 44.57)

Nothing seemed to change, the GUI still doesn’t enable

This is what is printing in output:

Summary

image

This is area of code where the printing is coming from

print("Working up till typing")
				
				positionDialogue()
				
				print("What")
				
				print(tostring(dialogue)) -- prints the value of the variable 
				
				if dialogue then
					typing(dialogue[currentNPC]["Page1"][1], dialogue[currentNPC]["Page1"][2])
				end

This means that calling positionDialogue is printing?

1 Like

Yes, and try following @skillednames advice as well

1 Like

I’m sorry I am far more of a builder than a scripter as you can see above lol

How would I go about giving the NPC a value based on its position?

This is what I’d do,
If other scripts are going to access the point, I’d make an IntValue within the NPC
When you move an NPC to a point, change the IntValue to a unique value
Now, whenever you want to check the position, simply check if its the unique value.
Hope it helps!

1 Like

Instantly found out what was wrong:

You did:

local module = {

Feugazi = {Page1 = {"example text."}, Page2 = {"example text."},
	Page3 = {"example text."}}
	
}
return module 

instead, it’s supposed to be:

local module = {

module.Feugazi = {Page1 = {"example text."}, Page2 = {"example text."},
	Page3 = {"example text."}}
	
}
return module
2 Likes

Thanks to everyone help I was able to figure out the best way for me to track the position of the HumanoidRootPart.

I made a separate script to print the exact position of the part over 20 seconds

Summary
local Feugazi = workspace.NPCs.Feugazi
local Root = Feugazi.HumanoidRootPart
local Postion = Root.Position


for count = 1,20 do
	wait (1)
	print(Postion)
end
print(Postion)

I then was able to place the position into this function. This allowed the script to choose the correct module script to load the Dialogue into the GUI.

Summary
function positionDialogue()
	local currentNPCRoot = ebutton.Parent
	local currentPosition = currentNPCRoot

	if currentPosition.Position == Vector3.new( -15.5499992, 2.69999981, 44.5700035) then
		dialogue = require(script:WaitForChild("DialogueOne"))
	end

end

(The if/then statement can be copy and pasted as many times as need within the function so that many different Dialogues can be used.)

I then just called the function within the Dialogue version of the script, and loaded the correct Dialogue set into the players GUI when it’s needed

Summary
positionDialogue()				-- PRINTS THE POSITION OF THE NPC 

				if dialogue then
					typing(dialogue[currentNPC]["Page1"][1], dialogue[currentNPC]["Page1"][2])
				end
3 Likes