Help needed with automaticly spawning bear

local AnimalFolder = game.ServerStorage.Animals
local bear = AnimalFolder.Bear

local destroyTime = 150
local waitDelay = 3


if game.Workspace.Baseplate == nil then -- Check if the baseplate exist
	return
end


local function CreateNewPart()
	local NewBear = bear:Clone()
	NewBear.Parent = workspace
	local newCFrame = CFrame.new((math.random((-game.Workspace.Baseplate.Size.X / 2), (game.Workspace.Baseplate.Size.X / 2))), 16, (math.random((-game.Workspace.Baseplate.Size.Z / 2), (game.Workspace.Baseplate.Size.Z / 2))))
	NewBear:PivotTo(newCFrame)

	game.Debris:AddItem(NewBear, destroyTime)
end

local amount = 15 -- Amount of bear that can exist at a single time
local currentAmount = 0

while true do
	if currentAmount <= amount then
		currentAmount = currentAmount + 1
		CreateNewPart()
	end
	
	if currentAmount == amount then
		print("Bear spawning is at " .. amount .. ". Currently, bear aren't spawned anymore till bear is destroyed or deleted.")
	end
	task.wait(waitDelay)
end

game.Workspace.ChildRemoved:Connect(function(Child)
	if Child == ("Bear") then
		currentAmount = currentAmount - 1
		print("Bear removed, now creating new bear")
	end
end)

@DevForumWanu10

Polished script of mine. Will work straight out of the box.

local AnimalFolder = game.ServerStorage.Animals
local bear = AnimalFolder.Bear

local destroyTime = 150
local waitDelay = 1


if game.Workspace.Baseplate == nil then -- Check if the baseplate exist
	return
end

-- NOTE : PARENT THE BEAR TO FOLDER SO IT DOESN'T GET CONFUSED WITH PLAYER.CHARACTER NAMED "BEAR"

local newFolder = Instance.new("Folder")
newFolder.Parent = game.Workspace
newFolder.Name = "Workspace.Bear"

local function CreateNewPart()	
	local NewBear = bear:Clone()
	
	if not newFolder:IsA("Folder") then -- check if character named bear exist. IF yes, check if it's a folder
		return
	end
	
	NewBear.Parent = newFolder
	local newCFrame = CFrame.new((math.random((-game.Workspace.Baseplate.Size.X / 2), (game.Workspace.Baseplate.Size.X / 2))), 16, (math.random((-game.Workspace.Baseplate.Size.Z / 2), (game.Workspace.Baseplate.Size.Z / 2))))
	NewBear:PivotTo(newCFrame)

	game.Debris:AddItem(NewBear, destroyTime)
end

local amount = 15 -- Amount of bear that can exist at a single time
local currentAmount = 0

game.Workspace.ChildAdded:Connect(function(child)
	print(child.Name) -- Expected is Bear, other than that will be Player.Character.
end)

newFolder.ChildRemoved:Connect(function(child)
	if child.Name == ("Bear") then -- Expected is Bear.
		currentAmount = currentAmount - 1
	end
end)

while true do
	if currentAmount < amount then
		currentAmount = currentAmount + 1
		CreateNewPart()
		print(currentAmount)
	end
	
	if currentAmount == amount then
		print("Bear spawning is at " .. amount .. ". Currently, bear aren't spawned anymore till bear is destroyed or deleted.")
	end
	task.wait(waitDelay)
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.