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()
local SoldierCount = 0
local Children = workspace:GetChildren()
for Index, Object in next, Children do
if (Object:IsA("Model") and (Object.Name == "Melee Soldier")) then
SoldierCount = (SoldierCount + 1)
end
end
return SoldierCount
end
Now we need to define a function to set the value of your IntValue to the number of soldiers:
local function SetNumberOfSoldiers()
local SoldierValue = script:FindFirstChild("Number of mobs")
if SoldierValue then
SoldierValue.Value = GetNumberOfMeleeSoldiers()
end
end
Now we need to call the function:
SetNumberOfSoldiers()
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)
workspace.ChildRemoved:connect(SetNumberOfSoldiers)
Final code:
local function GetNumberOfMeleeSoldiers()
local SoldierCount = 0
local Children = workspace:GetChildren()
for Index, Object in next, Children do
if (Object:IsA("Model") and (Object.Name == "Melee Soldier")) then
SoldierCount = (SoldierCount + 1)
end
end
return SoldierCount
end
local function SetNumberOfSoldiers()
local SoldierValue = script:FindFirstChild("Number of mobs")
if SoldierValue then
SoldierValue.Value = GetNumberOfMeleeSoldiers()
end
end
SetNumberOfSoldiers()
workspace.ChildAdded:connect(SetNumberOfSoldiers)
workspace.ChildRemoved:connect(SetNumberOfSoldiers)
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
--//
@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!
That was the whole comment. Also, using @ doesn't give the other player a notification.
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.