I’m trying to make a counter, each player has a permission system that inserts a value with their names inside a part when placed, the script should change the number value in the leaderboard every time a part is removed or added, but the script is located in a local script and when I tried switching to script it just didn’t work, can someone help me with that?
Note: the script its located at StarterPlayerScripts
local active = false
local baseParts = {}
local awaiting = 0
function fix(num)
num = tostring(num)
num = (num:reverse())
local newNum = ""
for i = 1,#num do
newNum = (newNum .. num:sub(i,i))
end
newNum = newNum:reverse()
if (newNum:sub(1,1) == ",") then
newNum = newNum:sub(2)
end
return newNum
end
function count()
if (active) then
awaiting = (awaiting+1)
return
end
active = true
wait()
baseParts = {}
local function scan(parent)
for _,v in pairs(parent:GetChildren()) do
if v:IsA("BasePart") and v:FindFirstChild("PartOwner") and v.PartOwner.Value == script.Parent.Parent.Name then
table.insert(baseParts,v)
end
scan(v)
end
end
scan(game.Workspace)
local n = #baseParts
script.Parent.Parent:WaitForChild("leaderstats").Counter.Value = (fix(n))
for i = 1,awaiting do
count()
end
awaiting = 0
active = false
end
function setup()
count()
game.Workspace.DescendantAdded:connect(count)
game.Workspace.DescendantRemoving:connect(count)
end
setup()