Need help with "fixing" my spawn script

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’d like to “fix” my prey spawn script.

  2. What is the issue? Include screenshots / videos if possible!
    The script uses RunService.Heartbeat and in order to stop the prey from overspawning I disconnect the function after it spawns. Because of this, the function won’t run again and the prey wont respawn.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried moving the disconnect somewhere else but that doesn’t fix anything. Idk any other ways to stop the prey from overspawning other than to disconnect the function. I tried to “reconnect” the function after it disconnected, but I’m not sure if that is very effective.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local Prey = game.ReplicatedStorage.Prey
local PreySpawn = script.Parent

local RunService = game:GetService("RunService")
local loopCount = 25
local Count = 0

local Players = game:GetService("Players")

local connection 

function onHeartbeat(step)
	
	if Count < loopCount then
		Count = Count + 1
		
	else

		for i, v in pairs (Players:GetPlayers()) do --Getting the current players in the server
			local plr = v
			local char = plr.Character
			local HRP = char.HumanoidRootPart
			
			local SpawnDistance = (HRP.Position - PreySpawn.Position).magnitude
			
			if SpawnDistance >= 10 then --if the player is 10 or more "studs" away
				
				local NewPrey = Prey:Clone()
				NewPrey.Parent = game.Workspace
				
				NewPrey:SetPrimaryPartCFrame(PreySpawn.CFrame)
				print(NewPrey.HumanoidRootPart.CFrame) -- It prints 408, 258.625, -151.5, 1, 0, 0, 0, 1, 0, 0, 0, 1 Idk what all the 0s and 1s are tho
			end
			
		end
		connection:disconnect()
	end
	
	
end

connection = RunService.Heartbeat:Connect(onHeartbeat)

There are no errors in the output and things print fine.
If you need more/different information then please ask!

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

I made a similar script for an old project. How I did it is I made a while loop to spawn them in every few seconds/minutes. You can make a folder in the workspace called “Prey” and parent all of them to it. You can use this:

local spawnedPrey = #game.Workspace:WaitForChild("Prey"):GetChildren()

Using that code, you can figure out how many prey are already spawned, and if it is too much, do nothing, if it is low enough to spawn another, spawn it. Since it is a loop it will keep spawning them in a certain time and it will not spawn too many.

Wouldn’t I have to rewrite the entire script then? Couldn’t I still use Runservice and Replicated storage?
Wouldn’t I also have to make multiple variables then? One for the clone, One for the original and one for the one in workspace?

You parent each one you spawn to the folder, so you can get the amount spawned. Using this, you can figure out how many are spawned so you can only spawn one in if there is little enough where it will not lag.

But if it’s parented to a folder then won’t it disappear from the workspace?

The folder is in the workspace, so they will still be visible

I might use something like that for now but I wanna make a system that checks how far away the player is instead of checking how many prey are in the world, this way I can install multiple spawns. Do you know how I can fix to my script then to do that?

You can combine the two, I showed above how to check how many there are. Do an if statement checking if the count is high or not. You can check the distance AND how many animals are already spawned.

I did what you asked I think-
I remade the script kinda but it doesn’t work…

local Prey = game.ReplicatedStorage.Prey
local PreySpawn = script.Parent
local RandomRock = game.Workspace.RandomRock
local Rabbits = game.Workspace.Rabbits --folder for the prey

local RunService = game:GetService("RunService")
local loopCount = 25
local Count = 0

local Players = game:GetService("Players")

local connection 

function onHeartbeat(step)
	if Count < loopCount then
		Count = Count + 1
		
	else --if the count = the loopcount
		for i, player in pairs(Players:GetPlayers()) do --we want to get the players and see if any are nearby
			
			local Char = player.Character
			local HumRootPart = Char.HumanoidRootPart
			
			local SpawnDistance = (HumRootPart.Position - PreySpawn.Position).magnitude --Using magnitude allows us the check the distance
			
			if SpawnDistance >= 10 then --if the player is far away enough 
				
				for i, v in pairs(Rabbits:GetChildren()) do --check the folder and see if there is anything parented to it
					print(v)

					if #v <= 1 then --if there is less than or equal to 1 prey 
						
						local NewPrey = Prey:Clone()
						NewPrey.Parent = Rabbits
						local x = math.random(RandomRock.Position.X, PreySpawn.Position.X)
						local z = math.random(RandomRock.Position.Z, PreySpawn.Position.Z)
						
						NewPrey.Position = Vector3.new(x,259.127,z)
					end
		        end
		    end
		end
	end
	
end

connection = RunService.Heartbeat:Connect(onHeartbeat)

To be fair, it was very confusing but what I was trying to do here was have a countdown and then check if there are any players nearby, then check if there was 1 or less than 1 prey inside the folder. If there was then it would clone the prey but because there would be more than one, I wanted to spawn it in a certain area randomly. I’m not sure if this was the right thing to do though. Nothing happens, nothing prints and no errors pop up.

This one will spawn one in every time the for loop runs.

Right, thats what I think its supposed to do, but it doesn’t work and I cant figure out why.