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

Trying to make a script that limits the objects in a model?

Hi, I'm a beginner scripter and I'm trying to write a script that deletes an object in a model if the model already has more than 10 of the object in it. Here is my script:


if script.Parent:GetChildren() > 10 then 
local object = script.Parent:FindFirstChild("Object")
object:Destroy()
end

Thanks

1 comment

0

"Object" is pretty ambigious. So lets instead say, if each item was called "Part" you would replace "Object" with "Part". Also, make sure you indent youre code

2 answers

✓ Accepted answer

You should first check what events you can use for this task. In your case you can use ChildAdded or DescendantAdded.

Lastly GetChildren returns an array which youcan get the length of using #.

Example:-

local model = script.Parent
local limit = 10 -- child limit
limit = limit  + 1 -- offset as the script is a child

model.ChildAdded:Connect(function(ins) -- the event passes the item being added
	-- check current item count
	if #model:GetChildren() > limit then
		ins:Destroy()
	end
end)

I hope this helps.

1 comment

0

Thanks Ill make sure to find out about tables too

Bin = script.Parent
if Bin:GetChildren() > 10 then
	local object = Bin:FindFirstChild("Part")
	object:Destroy()
end

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