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

Counting a specific model in workspace?

I'm trying to write a script that keeps count of Npcs in workspace that are named "Melee Soldier" but it doesn't work. Is there a mistake that I made?

while wait() do 
local n = script["Number of mobs"]-- this is a intvalue value

local g = game.Workspace:GetChildren()

for i = 1, #g do
if g[i].Name == "Melee Soldier" then
n.Value = #g[i]
end
if n.Value< = 0 then
print("No enemies")
end
end
end

1 answer

✓ Accepted answer

Hi there. Please remember it's always better to use clear variable names & use plenty of whitespace to make your code easier to read. I'm not able to read your code in it's current state. However, I will explain to you how to do what you want.

Now, what you want to do is have the number value set to a constant value of how many Melee Soldiers are in the game. To do this, we will make a function to get the number of soldiers:

local function GetNumberOfMeleeSoldiers() --\\Define a function to get the number of soldiers

	local SoldierCount = 0 --\\Define SoldierCount so we can keep track of # of soldiers.

	local Children = workspace:GetChildren() --\\Get a table of the workspace's children.

	for Index, Object in next, Children do --\\Iterate through the table of children.

		if (Object:IsA("Model") and (Object.Name == "Melee Soldier")) then --\\ Check if the object is a model, and if so check if the name of the model is Melee Soldier.

			SoldierCount = (SoldierCount + 1) --\\It checks out, +1 to the number of soldiers.

		end --\\End the if statement
	end --\\End the loop

	return SoldierCount --\\Return the number of soldiers; we are done iterating.

end --\\End the function definition

Now we need to define a function to set the value of your IntValue to the number of soldiers:

local function SetNumberOfSoldiers() --\\Define the function to set the number of soldiers

	local SoldierValue = script:FindFirstChild("Number of mobs") --\\Attempt to locate the soldier value

	if SoldierValue then --\\Make sure the SoldierValue exists

		SoldierValue.Value = GetNumberOfMeleeSoldiers() --\\Set the value of it to the number of soldiers

	end --\\End the if statement
end --\\End the function definition

Now we need to call the function:

SetNumberOfSoldiers() --\\Call the function to get the initial value of soldiers

But now we need to call the function whenever the number of children in workspace changes, instead of using a loop. This is much more efficient!

workspace.ChildAdded:connect(SetNumberOfSoldiers) --\\Call our function whenever a child is added

workspace.ChildRemoved:connect(SetNumberOfSoldiers) --\\Call our function whenever a child is removed

Final code:

local function GetNumberOfMeleeSoldiers() --\\Define a function to get the number of soldiers

	local SoldierCount = 0 --\\Define SoldierCount so we can keep track of # of soldiers.

	local Children = workspace:GetChildren() --\\Get a table of the workspace's children.

	for Index, Object in next, Children do --\\Iterate through the table of children.

		if (Object:IsA("Model") and (Object.Name == "Melee Soldier")) then --\\ Check if the object is a model, and if so check if the name of the model is Melee Soldier.

			SoldierCount = (SoldierCount + 1) --\\It checks out, +1 to the number of soldiers.

		end --\\End the if statement
	end --\\End the loop

	return SoldierCount --\\Return the number of soldiers; we are done iterating.

end --\\End the function definition

local function SetNumberOfSoldiers() --\\Define the function to set the number of soldiers

	local SoldierValue = script:FindFirstChild("Number of mobs") --\\Attempt to locate the soldier value

	if SoldierValue then --\\Make sure the SoldierValue exists

		SoldierValue.Value = GetNumberOfMeleeSoldiers() --\\Set the value of it to the number of soldiers

	end --\\End the if statement
end --\\End the function definition


SetNumberOfSoldiers() --\\Call the function to get the initial value of soldiers

workspace.ChildAdded:connect(SetNumberOfSoldiers) --\\Call our function whenever a child is added


workspace.ChildRemoved:connect(SetNumberOfSoldiers) --\\Call our function whenever a child is removed

As a bonus, you could make it only call SetNumberOfSoldiers if the child was a Melee Soldier to save the heavy performance cost of iterating through workspace, if it has a lot of objects. As another bonus, you could add a yield every 15 iterations or so, in case the script crashes due to the workspace having too many objects.

I'll leave that up to you though, that'll help you learn Lua to the fullest! :D

EDIT: added more white-space; due to the number of comments, it was a bit unreadable.

4 comments

0

--//

0

@wfvj014 Could you please re-comment that? It seems you didn't finish your sentence. Also, did some more updates; hadn't realized it was that unreadable. Sorry!

0

That was the whole comment. Also, using @ doesn't give the other player a notification.

0

Hmm, are they automatic then? I'm new to the site, I'm just now learning how to properly do this thing. Also, what does the comment mean? I don't quite understand what your getting at.

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