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
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.