Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make this magnitude script work without duplicating it?

I'm making a dialog system and I have 2 NPCs. It's a local script in StarterGui, and I can't seem to make it work for more than one NPC.

repeat wait(1) until game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

local NPC = game.Workspace.NPC -- folder
local UIS = game:GetService("UserInputService")
local radius = 7
local interact = script.Parent:WaitForChild("Interact")
local debounce = false

local dialog = game.ReplicatedStorage.Dialog

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local root = char:WaitForChild("HumanoidRootPart")

while wait(0.5) do
	for i, v in pairs(NPC:GetChildren()) do
		if (root.Position - v.HumanoidRootPart.Position).magnitude <= radius then
			interact.TextLabel.Visible = true
			
			UIS.InputBegan:Connect(function(key, gpe)
				if gpe then return end
				
				if key.KeyCode == Enum.KeyCode.B and not debounce and interact.TextLabel.Visible == true then
					debounce = true
					interact.TextLabel.Visible = false
					game.ReplicatedStorage.Remotes.StartDialog:FireServer(v.NPCName.Value)
					print(debounce)
					wait(1)
					debounce = false
				end
			end)
		else
			interact.TextLabel.Visible = false
		end
	end
end

I don't want to duplicate it in order to make it work.

1 answer

✓ Accepted answer

Given I don't know how your game is structured, I wasn't too sure how to go about this, but I hope this works.

repeat 
	wait() 
until game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

local NPC = workspace:WaitForChild("NPC") -- folder
local UIS = game:GetService("UserInputService")
local RADIUS = 7
local search_radius = RADIUS
local interact = script.Parent:WaitForChild("Interact")
local debounce = false

local dialog = game.ReplicatedStorage:WaitForChild("Dialog")

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local root = char:WaitForChild("HumanoidRootPart")

function search_radius()
	local npc
	for i, v in pairs(workspace.NPC:GetChildren()) do
		local hrp = v:FindFirstChild("HumanoidRootPart")
		if hrp then
			local dist = (root.Position - hrp.Position).Magnitude
			
			if dist <= search_radius then
				npc = v
				search_radius = dist
			end
		end
 	end
	search_radius = RADIUS
	return npc
end

UIS.InputBegan:Connect(function(key, gpe)
	local npc = search_radius()
	if key.KeyCode == Enum.KeyCode.B and not debounce and interact.TextLabel.Visible and not gpe and npc then
		debounce = true
		interact.TextLabel.Visible = false
		
		game.ReplicatedStorage.Remotes.StartDialog:FireServer(npc.NPCName.Value)
		print(debounce)
		wait(1)
		debounce = false
	end
end)

while wait(0.5) do
    for i, v in pairs(NPC:GetChildren()) do
        if (root.Position - v.HumanoidRootPart.Position).magnitude <= RADIUS then
            interact.TextLabel.Visible = true
        else
            interact.TextLabel.Visible = false
        end
    end
end
=

3 comments

0

it still doesn't work but thanks for trying.

1

somehow i made it work. guess it was supposed to be elseif instead of else

0

good to hear!

This discussion is preserved as it was. New answers, comments, and votes are closed.