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

How do I disable a script after an amount of models have been detected in the workspace?

I have a script that clones random zombie models in a random position and into the workspace.

--variables
topLeft = Vector3.new(-85.3, 0.5, -30.9) --Top left corner of the 2d region
bottomRight = Vector3.new(-43.2, 0.5, 6.9) --Bottom right corner of the 2d region
az = game.ReplicatedStorage.zombiemodels.A
bz = game.ReplicatedStorage.zombiemodels.B
cz = game.ReplicatedStorage.zombiemodels.C
dz = game.ReplicatedStorage.zombiemodels.D
ez = game.ReplicatedStorage.zombiemodels.E
fz = game.ReplicatedStorage.zombiemodels.F
gz = game.ReplicatedStorage.zombiemodels.G

singletonRandom = Random.new(tick())

--Function for the random number

function GetRandom(Min,Max)
	return singletonRandom:NextNumber(Min,Max)
end

--Making the cloning items

PlayingFieldHeight = 15
while true do
 local phrases = { az, bz, cz, dz, ez, fz, gz }
 print ( phrases[ math.random( #phrases ) ] )
 model = (phrases[math.random(#phrases)]):clone()
 model.Parent = game.Workspace
 model.PrimaryPart = model.HumanoidRootPart
 model:SetPrimaryPartCFrame(CFrame.new(GetRandom(topLeft.X,bottomRight.X),PlayingFieldHeight,GetRandom(topLeft.Z, bottomRight.Z)))
 wait(2)
end

I am very very new to scripting so please cut me some slack here, but I need to find out how I can count how many models are in the workspace with the corresponding names "A, B, C, D, E, F, G" and then disable the script when the number has reached the threshold I give it. Any help on how I can do this? I have no idea.

2 answers

I don't understand your script but I'll just do what the question asked

for i,v in pairs(workspace:GetChildren()) do
	if v:IsA("Model") then
		local number = #v
		if number >= 5 then -- If there are more than 5 models
			script.Disabled = true
		end
	end
end

2 comments

0

Probably should have clarified what the script does. The script creates a 3d space for zombies to randomly spawn inside of and then chooses a random model to spawn and spawns it in a random area.. I will update you on whether or not this works. Thank you! 

0

Unfortunately the script does not work. Either that or I am just doing it wrong, idk.

local s = --Script here
while true do
    wait()
    if tonumber[game.Workspace:GetChildren()] >= 20 then
        s.Disabled = true
    end
end

That is what I would do.

4 comments

0

This counts all objects in workspace, not just models.

0

Change 20 to whatever you want.

0

I don't believe it works. The script gives me errors when I put it in an individual script but the errors go away if I put the script into the original, but still doesn't work. "Workspace.Script:4: attempt to index global 'tonumber' (a function value)"

0

Oof,

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