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

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

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?

1 comment

0

sorry, I dont know why it jumbled the script together.

1 answer

✓ Accepted answer

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.

2 comments

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?

0

Try using Remote Event/Function as a way to tell the server

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