Hello. I'm trying to make a dialog system with an NPC where I use magnitude to detect if the player is close to the NPC. If he is, then he has to press e and a gui will show up showing "press e to interact". Everything works fine, but while the npc is chatting, the interact gui keeps appearing.
-- Script inside the model
local UIS = game:GetService("UserInputService")
local debounce = false
local modelName = "Dave"
local model = script.Parent
local interact = game.ReplicatedStorage.Interact
local dialog = game.ReplicatedStorage.Dialog
local dist = 5
while true do
for _, v in pairs(game.Players:GetChildren()) do
local char = v.Character or v.CharacterAdded:Wait()
local playerGui = v:WaitForChild("PlayerGui")
if (char:WaitForChild("HumanoidRootPart").CFrame.p - model.HumanoidRootPart.CFrame.p).magnitude <= dist then
game.ReplicatedStorage.Remotes.TalkDave:FireClient(v, dist)
else
interact.Parent = game.ReplicatedStorage
end
wait()
end
wait()
end
-- Local script in startergui
local UIS = game:GetService("UserInputService")
local debounce = false
local modelName = "Dave"
local model = game.Workspace.Dave
local interact = game.ReplicatedStorage.Interact
local dialog = game.ReplicatedStorage.Dialog
local player = game.Players.LocalPlayer
game.ReplicatedStorage.Remotes.TalkDave.OnClientEvent:Connect(function(dist)
local char = player.Character or player.CharacterAdded:Wait()
local playerGui = player:WaitForChild("PlayerGui")
local humanoid = char:WaitForChild("Humanoid")
if dialog.Parent == playerGui then
interact.Parent = nil
end
if (char:WaitForChild("HumanoidRootPart").CFrame.p - model.HumanoidRootPart.CFrame.p).magnitude <= dist then
interact.Parent = playerGui
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E and not debounce and interact.Parent == playerGui then
debounce = true
humanoid.WalkSpeed = 0
interact.Parent = nil
dialog.Parent = playerGui
local message = "Life in Saffron Village is so cool! We have a forest where we get our food from, and beyond the forest is Port Savaria, where we can go to other towns and cities."
dialog.NameBox.TextLabel.Text = modelName
for i = 1, #message do
dialog.Text.TextLabel.Text = string.sub(message, 1, i)
wait()
end
dialog.Continue.Visible = true
dialog.Cancel.Visible = true
dialog.Continue.MouseButton1Down:Connect(function()
dialog.Continue.Visible = false
dialog.Cancel.Visible = false
dialog.NameBox.TextLabel.Text = ""
dialog.Text.TextLabel.Text = ""
dialog.Parent = game.ReplicatedStorage
interact.Parent = game.ReplicatedStorage
humanoid.WalkSpeed = 30
end)
dialog.Cancel.MouseButton1Down:Connect(function()
dialog.Continue.Visible = false
dialog.Cancel.Visible = false
dialog.NameBox.TextLabel.Text = ""
dialog.Text.TextLabel.Text = ""
dialog.Parent = game.ReplicatedStorage
interact.Parent = game.ReplicatedStorage
humanoid.WalkSpeed = 30
end)
end
wait(1)
debounce = false
end)
else
interact.Parent = game.ReplicatedStorage
end
end)
I am aware of the while loop from the server script that causes it, but I can't find a way to make the interact gui stop appearing while in dialog.
