Need help with Organizing the code

I have 3 npc type here with different Tag

“Hostile”, “Friendly”, “Neutral” in order
which all share the same code but in their script in their separate model

Script
local Dummy = script.Parent.Parent
local humanoid = Dummy:WaitForChild("Humanoid")
local top = script.Parent.BillboardGui.HPbg.HPFront
local Name = script.Parent.BillboardGui.nameTag.TextLabel

local cs = game:GetService("CollectionService")
local hl = Instance.new("Highlight", Dummy)
hl.FillTransparency = 0.9
hl.OutlineTransparency = 0.5

local MainColor
local connection_health
local connection_maxhealth

local function SetName()
	Name.Text = Dummy.Name
end

local function TagCheck()
	if cs:HasTag(Dummy, "Hostile") then
		MainColor = Color3.fromRGB(255, 0, 0)
	elseif cs:HasTag(Dummy, "Friendly") then
		MainColor = Color3.fromRGB(0, 255, 0)
	elseif cs:HasTag(Dummy, "Neutral") then
		MainColor = Color3.fromRGB(255, 255, 255)
	else
		MainColor = Color3.fromRGB(255, 255, 255)
	end
end

local function SetColor()
	Name.TextColor3 = MainColor
	hl.FillColor = MainColor
	hl.OutlineColor = MainColor
	top.BackgroundColor3 = MainColor
end

local function update()
	top:TweenSize(UDim2.new(math.clamp(humanoid.Health / humanoid.MaxHealth, 0, 1), 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quart, 0.5, true)
end

local function connections()
	connection_health = humanoid:GetPropertyChangedSignal("Health"):Connect(update)
	connection_maxhealth = humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(update)
	update()
end

TagCheck()
SetColor()
SetName()
connections()

what the script does is create a highlight and Change the color theme of the dummy depending on their tag, it also responsible for updating the health bar gui
the result:

So, what’s the problem?
I feel like having 3 separate scripts that do the same thing for every dummy is a huge waste of performance and I want to convert this into one server script but I don’t know how to achieve that

I think that you can just copy and paste 3 times(dont copy the function or GetService) and each time set the dummy variable to the path of each dummy

but what if I have multiple copy of each type

I think that your TagCheck() can distinguish different type so just copy the amount of character

I did it! with trial and error and a help with a lot of for loop, now I can have multiple goober with their own color code and I only need one script for that

Give Color Script
local cs = game:GetService("CollectionService")

local npcFolder = workspace.NPC
local Hostile = {}
local Friendly = {}
local Neutral = {}

function TagsCheck()
	for i, npc in pairs(npcFolder:GetChildren()) do
		if cs:HasTag(npc, "Hostile") then
			table.insert(Hostile, npc)
		elseif cs:HasTag(npc, "Friendly") then
			table.insert(Friendly, npc)
		elseif cs:HasTag(npc, "Neutral") then
			table.insert(Neutral, npc)
		end
	end
end


function Remove(child)
	for i, v in pairs(Hostile) do
		if v == child then
			table.remove(Hostile, i)
		end
	end
	for i, v in pairs(Friendly) do
		if v == child then
			table.remove(Friendly, i)
		end
	end
	for i, v in pairs(Neutral) do
		if v == child then
			table.remove(Neutral, i)
		end
	end
end

function NameTag()
	for i, npc in pairs(npcFolder:GetChildren()) do
		if npc:IsA("Model") then
			local name = npc:WaitForChild("Head"):WaitForChild("BillboardGui"):WaitForChild("nameTag"):WaitForChild("TextLabel")
			name.Text = npc.Name
		end
	end
end

function SetColor(npc, Color)
	local name = npc:WaitForChild("Head"):WaitForChild("BillboardGui"):WaitForChild("nameTag"):WaitForChild("TextLabel")
	local top = npc:WaitForChild("Head"):WaitForChild("BillboardGui"):WaitForChild("HPbg"):WaitForChild("HPFront")
	local hl = Instance.new("Highlight", npc)
	hl.DepthMode = Enum.HighlightDepthMode.Occluded
	hl.FillTransparency = 0.9
	hl.OutlineTransparency = 0.5
	name.TextColor3 = Color
	hl.FillColor = Color
	hl.OutlineColor = Color
	top.BackgroundColor3 = Color
end

function ColorTag()
	for i, npc in pairs(Hostile) do
		SetColor(npc, Color3.fromRGB(255, 0, 0))
	end
	for i, npc in pairs(Friendly) do
		SetColor(npc, Color3.fromRGB(0, 255, 0))
	end
	for i, npc in pairs(Neutral) do
		SetColor(npc, Color3.fromRGB(255, 255, 255))
	end
end

function UpdateTable()
	npcFolder.ChildAdded:Connect(function(child)
		TagsCheck()
		NameTag()
		ColorTag()
	end)
	npcFolder.ChildRemoved:Connect(function(child)
		Remove(child)
	end)
end


TagsCheck()
NameTag()
ColorTag()
Health Bar Update Script
local connection_health
local connection_maxhealth

local Folder = workspace.NPC

local function update()
	for i, npc in pairs(Folder:GetChildren()) do
		if npc:IsA("Model") then
			local Humanoid = npc:WaitForChild("Humanoid")
			if Humanoid then
				for _, Top in pairs(npc:GetDescendants()) do
					if Top:IsA("Frame") and Top.Name == "HPFront" then
						Top:TweenSize(UDim2.new(math.clamp(Humanoid.Health / Humanoid.MaxHealth, 0, 1), 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quart, 0.5, true)
					end
				end
			end
		end
	end
end

local function connections()
	for i, npc in pairs(Folder:GetChildren()) do
		if npc:IsA("Model") then
			local Humanoid = npc:WaitForChild("Humanoid")
			if Humanoid then
				connection_health = Humanoid:GetPropertyChangedSignal("Health"):Connect(update)
				connection_maxhealth = Humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(update)
			end
		end
	end
	update()
end

connections()

hey, this isnt related but the font looks really good which one is it i could use it

image
Press Start 2P

1 Like