How would you make player replacement bots

Recently, I have been noticing that games have bots that fill missing players if the server isn’t full. These bots disappear and reappear every time a player joins or leaves the game. I wanna achieve something like this where realistic bots that do the tasks normal players would do in the game fill up the missing players in a server and leave/rejoin if players leaves/rejoins.


1 Like

Scripting the AI for these is VERY different depending on the game, so I can’t help you there.

However, I can give you a basic framework. Try something like this:

local maxServerSize = 50 -- Change this to your game's max server size
local spawnedFolder = workspace.Bots -- This will be where your bots are stored. Feel free to change this

local function spawnBot()
    -- A player left.
    -- Spawn a bot! Clone them from somewhere into spawnedFolder.
end

local function despawnBot()
    -- A player joined.
    -- Destroy a bot! Get a random bot from the spawnedFolder and destroy it.
end

local function update()
    if #spawnedFolder:GetChildren > maxServerSize - #game.Players:GetPlayers() then
        -- Despawn bots until the bots fill ONLY the needed slots
        repeat despawnBot() until #spawnedFolder:GetChildren == maxServerSize - #game.Players:GetPlayers()
    elseif #spawnedFolder:GetChildren < maxServerSize - #game.Players:GetPlayers()
        -- Spawn bots until the bots fill ONLY the needed slots
        repeat spawnBot() until #spawnedFolder:GetChildren == maxServerSize - #game.Players:GetPlayers()
    end
end

game.Players.PlayerAdded:Connect(update)
game.Players.PlayerRemoving:Connect(update)

This should work, but I haven’t tested it. Let me know if it does!

The bots will only be how many the max players are, since it doesn’t count for the other ones. When you leave the game, it doesn’t add one, and when someone joins, it doesn’t remove one either. Also you made some errors on the script, so I slightly modified it.

local maxServerSize = 10 -- Change this to your game's max server size
local spawnedFolder = workspace.Bots -- This will be where your bots are stored. Feel free to change this

local function spawnBot()
	-- A player left.
	-- Spawn a bot! Clone them from somewhere into spawnedFolder.
	local bot = game:GetService("ServerStorage").BotTemplate:WaitForChild("Killer"):Clone()
	bot.Parent = spawnedFolder
end

local function despawnBot()
	-- A player joined.
	-- Destroy a bot! Get a random bot from the spawnedFolder and destroy it.
	spawnedFolder:FindFirstChild("bot"):Destroy()
end

local function update()
	if #spawnedFolder:GetChildren() > maxServerSize - #game.Players:GetPlayers() then
		-- Despawn bots until the bots fill ONLY the needed slots
		repeat despawnBot() until #spawnedFolder:GetChildren() == maxServerSize - #game.Players:GetPlayers()
	elseif #spawnedFolder:GetChildren() < maxServerSize - #game.Players:GetPlayers() then
		-- Spawn bots until the bots fill ONLY the needed slots
		repeat spawnBot() until #spawnedFolder:GetChildren() == maxServerSize - #game.Players:GetPlayers()
end
end

game.Players.PlayerAdded:Connect(update)
game.Players.PlayerRemoving:Connect(update)
1 Like