My GUI tweening system is only working for one NPC

I have been trying to create an NPC interaction system, where a mini GUI pops up stating ‘Press E to interact with NPC.’ It all works - but only for one NPC. The mini GUI pop-up for all the other NPC’s are not functioning. There is nothing popping up in the Output which is really annoying. Here’s the LocalScript plus a video:

    local UserInputService = game:GetService("UserInputService")
    local Player = game.Players.LocalPlayer
    local isinrageof = nil
    local ischatting = false
    local line = 1
    local istalking = false
    local dialogues = nil

local function onInputBegan(input,gameProcessed)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		local keyPressed = input.KeyCode
		if keyPressed == Enum.KeyCode.E then
			if isinrageof ~= nil and not istalking then
				if ischatting == false then
					line = 1
					ischatting = true
					dialogues = require(isinrageof.Lines)
					Player.Character.Humanoid.WalkSpeed = 0
					script.Parent.DialogueFrame.Dialogue.Text = ""
					script.Parent.DialogueFrame.NPCName.Text = isinrageof.Name
					script.Parent.DialogueFrame:TweenPosition(UDim2.new(0.099,0,0.809,0), "Out", "Back", .2, true)
					script.Parent.Keybind:TweenPosition(UDim2.new(0.375,0,1.1,0),"Out","Back",.3)
					script.Parent.Keybind.Visible = false
					script.Parent.KeybindEnabled.Value = false
					wait(.4)
					local CurLine = dialogues[line]
					istalking = true
					for i=1,#CurLine[1] do
						script.Parent.DialogueFrame.Dialogue.Text = string.sub(CurLine[1],0,i)
						wait(CurLine[2])
					end
					if CurLine[3] then
						CurLine[3]()
					end
					istalking = false
					line = line + 1
				else
					local CurLine = dialogues[line]
					if line > #dialogues then
						
						script.Parent.DialogueFrame:TweenPosition(UDim2.new(0.099,0,1.2,0), "Out", "Back", .2, true)
						script.Parent.Keybind:TweenPosition(UDim2.new(0.375,0,0.877,0),"Out","Back",.3)
						script.Parent.Keybind.Visible = true
						script.Parent.KeybindEnabled.Value = true
						Player.Character.Humanoid.WalkSpeed = 16
						dialogues = nil
						ischatting = false
					else
						local CurLine = dialogues[line]
						istalking = true
						for i=1,#CurLine[1] do
							script.Parent.DialogueFrame.Dialogue.Text = string.sub(CurLine[1],0,i)
							wait(CurLine[2])
						end
						if CurLine[3] then
							CurLine[3]()
						end
						istalking = false
						line = line + 1
					end
				end
			end
		end
	end
end

UserInputService.InputBegan:Connect(onInputBegan)

while true do
	wait()
	if ischatting == false then
		for i,v in pairs(workspace.NPCs:GetChildren()) do
			if(Player.Character.HumanoidRootPart.Position-v.Torso.Position).magnitude <= 6 then
				isinrageof = v
				script.Parent.Keybind:TweenPosition(UDim2.new(0.375,0,0.877,0),"Out","Back",.3)
				script.Parent.Keybind.TextLabel.Text = "Press 'E' to interact with "..v.Name.."."
			else
				script.Parent.Keybind:TweenPosition(UDim2.new(0.375,0,1.1,0),"Out","Back",.3)
			end
		end
	end
end

1 Like

Is the other NPC parented to the same folder/group?

Yes. This is really annoying me because I have no idea why the script is only making it work for one NPC.

Following the interaction with the NPC on the right (the one that’s not working), the mini GUI pops up for less than a second before it goes down back to it’s original position.

Can you send me a repo file so I can debug it please?

Through your messages on DevForum or somewhere else?

This is most likely happening cause its the same GUI and while you are walking away from 1 npc it will close the gui but when you try open it, the script seeing that you are away from the other npc is not opening the gui again

Try renaming the gui to NPC.Name … “GUI” then make it close when player walks away with the same method

The script is meant to change the name in a certain area of the GUI from the NPC’s Model name. This is the part of the script that’s confusing:

while true do
wait()
if ischatting == false then
	for i,v in pairs(workspace.NPCs:GetChildren()) do
		if(Player.Character.HumanoidRootPart.Position-v.Torso.Position).magnitude <= 6 then
			isinrageof = v
			script.Parent.Keybind:TweenPosition(UDim2.new(0.375,0,0.877,0),"Out","Back",.3)
			script.Parent.Keybind.TextLabel.Text = "Press 'E' to interact with "..v.Name.."."
		else
			script.Parent.Keybind:TweenPosition(UDim2.new(0.375,0,1.1,0),"Out","Back",.3)
			end
		end
	end
end

The LocalScript requests the GUI to pop up with a different name when that NPC is approached.

As @anon20884738 stated, you were checking if you were a certain distance away from both of the NPCs at the same time.

I fixed this by just using the for loop through the NPC model to check if you are close to one NPC. If you are, it breaks the loop and sets a bool value to true. (You will see this in the file)

I also went and tidied your code up a little bit (formatting) to make it easier to read :slight_smile:

Dialogue.rbxl (569.1 KB)

4 Likes

Thank you so much for helping me!

1 Like