Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you count the amount of the same model existing in a game at once in a script?

Asked by 3 years ago
Edited by Gey4Jesus69 3 years ago

I want it so that for every model named "Plant" gives 100 "Credits" per 1.5 seconds.

I have the actual money giving script done and perfect, it gives 10 credits every 1.5 seconds, now I want it to add 150 more credits per Plant that exists simultaneously in game.

I have it to where it prints whatever the value of "amount" is every time it adds Credits, it just continuously adds 150 to the already existing 150 making it go up to numbers like 1000 within seconds, this is because it is in a loop, but the script doesn't actually work unless it is in the loop.

Heres the script:

amount = 10
timedelay = 1.5
currencyname = "Credits"
teamcore = 0 -- ignore this

while true do 
    wait(timedelay)
    for i,v in pairs(game.Players:GetPlayers()) do
        if v:FindFirstChild("leaderstats") then
            v.leaderstats[currencyname].Value = v.leaderstats[currencyname].Value + amount
            local cash = workspace:FindFirstChild("Plant")
    if cash then
        yes = true 
                if yes == true then
                    amount = amount + 150
                    print(amount)
                end
            end
        end
    end
end

any help please?

0
sorry, I dont know why it jumbled the script together. nizzadien33 2 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Try using Instance:GetDescendants() function, returns an array that contains all descendants of selected object. FYI:

https://developer.roblox.com/en-us/api-reference/function/Instance/GetDescendants

local Models = game.Workspace:GetDescendants() -- up to you
local Plants = {}

for i, model in pairs(Models) do
    if model:IsA("Model") then
        if model.Name == "Plant" then
            table.insert(Plants, model)
        end
    end
end

print("There are " ..#Plants) -- prints how many plants are in your game.

PS: If this meant to count a player's owned plant then put this in a local script and tell the server by using remote events/functions.

0
It says the exact number that exist! Thanks! But now my brain went blank, and I can't figure out how to make it so that it adds 100 Credits for every existing plant? nizzadien33 2 — 3y
0
Try using Remote Event/Function as a way to tell the server Feelings_La 399 — 3y
Ad

Answer this question