Bots wont spawn on player joined

  1. What do you want to achieve? I want it so whenever a player joins, there will be a multiplier on how much bots would spawn in a server.

  2. What is the issue? It’s just simply not doing what I said, I’m pretty sure it has something to do (mainly) with the addNextbots() function. The random Index? The Enum Module? I don’t know. Please tell me the problem, I’d want to fix it myself so I could learn more.

  3. What solutions have you tried so far? I did try looking up something related to this but nothing appears.

nbSpawn Module:

local nbSpawn = {}

local PhysicsService = game:GetService("PhysicsService")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Modules = ReplicatedStorage:WaitForChild("Modules")
local Values = ReplicatedStorage:WaitForChild("Values")

local Assets = ReplicatedStorage:WaitForChild("Assets")
local botTemplate = Assets:WaitForChild("botTemplate")

local EnumModule = require(Modules.nbEnum)
local nbEnum = EnumModule.Enum

local nextbotMulti = Values:WaitForChild("nextbotMulti")

local function ifExists(nbName)
	if nbEnum.nextbots[nbName] then
		return true, nbEnum.nextbots[nbName]
	else
		warn("ifExists(): failed to find nextbot '"..nbName.."' in nextbot data.")
		return false, nil
	end
end

local function setNBThings(nb, nbName)
	if nbEnum.nextbots[nbName] then
		local nbData = nbEnum.nextbots[nbName]

		local nbTemp : Model = nb:Clone()
		nbTemp.Name = nbName
		
		local _set = nbTemp:FindFirstChild("set_startpos")
		
		local specialVal = nbTemp:GetAttribute("special")
		specialVal = nbData.special
		
		local startposVal = nbTemp:GetAttribute("StartPos")
		startposVal = nbData.startpos
		wait()
		_set.Enabled = true

		local nbRoot = nbTemp:FindFirstChild("HumanoidRootPart")
		local nbNoid = nbTemp:FindFirstChild("bot")
		nbNoid.WalkSpeed = nbData.botWalkspeed
		nbNoid.JumpPower = nbData.botJumpPower

		local nbIcon = nbRoot:FindFirstChild("icon")
		local nbIconFR = nbIcon:FindFirstChild("ImageLabel")
		nbIconFR.Image = nbData.botImg

		local nbTheme = nbRoot:FindFirstChild("theme")
		nbTheme.SoundId = nbData.botTheme

		local themeDist = nbTheme:FindFirstChild("Dist") :: DistortionSoundEffect
		themeDist.Level = nbData.themeDist

		return true, nbTemp
	else
		warn("setNBThings(): Neither couldn't set things for '"..nbName.."' nextbot, or failed to find nextbot '"..nbName.."' in nextbot data.")
		return false, nil
	end
end



function nbSpawn.spawnNB(nb, map)
	local exists, nbData = ifExists(nb)
	if exists then
		print("Nextbot exists:", nb)

		local template = botTemplate:WaitForChild("bottemp")
		local success, spawnedNB : Model = setNBThings(template, nb)

		if success then
			print("Successfully set things for", nb)

			local rootP : BasePart = spawnedNB:FindFirstChild("HumanoidRootPart") :: BasePart
			local lightingBeam : Beam = rootP:FindFirstChild("lightning") :: Beam
			local beamAtt = rootP:FindFirstChild("beam1")

			local sfx : Sound = beamAtt:FindFirstChild("sfx")
			local vfx : ParticleEmitter = beamAtt:FindFirstChild("vfx")

			spawn(function()
				lightingBeam.Enabled = true
				sfx:Play()
				vfx:Emit(250)
				wait(.35)
				lightingBeam.Enabled = false
			end)

			spawnedNB.Parent = workspace.bots
			spawnedNB.PrimaryPart:SetNetworkOwner(nil)

			for _, v in pairs(spawnedNB:GetDescendants()) do
				if v:IsA("BasePart") then
					PhysicsService:SetPartCollisionGroup(v, "bot")
				elseif v:IsA("Script") then
					if v.Name ~= "set_startpos" then
						v.Enabled = true
					end
				end
			end

			print("Successfully spawned", nb)
		else
			warn("Failed to set things for", nb)
		end
	else
		warn("Nextbot does not exist:", nb)
	end
end

function nbSpawn.RemoveAllBots()
	local botsFolder = workspace.bots
	
	if botsFolder then
		for _, v in pairs(botsFolder:GetChildren())do
			if v:IsA("Model") then
				v:Destroy()
			end
		end
	end
end

function addNextbots()
	local defaultAmountNBs = 2

	if nbEnum.nextbots and #nbEnum.nextbots > 0 then
		local defaultTimesMulti = defaultAmountNBs * nextbotMulti.Value

		for i = 1, defaultAmountNBs do
			local randomBot = nbEnum.nextbots[math.random(1, #nbEnum.nextbots)]

			if randomBot then
				print("Spawning nextbot :", randomBot)
				nbSpawn.spawnNB(randomBot)
			else
				print("Error: Invalid nextbot not found in nbEnum.nextbots.")
			end
		end
	else
		print("Error: nbEnum.nextbots is empty or not defined. No nextbots to spawn.")
	end
end

nextbotMulti:GetPropertyChangedSignal("Value"):Connect(addNextbots)

return nbSpawn

nbEnum Module:

local nbEnum = {}

local v3 = Vector3.new

nbEnum.Enum = {
	nextbots = {
		["botTemplate"] = {
			botImg = "rbxassetid://5902417546",
			imgAnimated = false,

			botTheme = "rbxassetid://14980518563",
			themeDist = 0.25,
			botJSSFX = "rbxassetid://14980519309",
			
			botWalkspeed = 999,
			botJumpPower = 999,
			
			bloodmoonWalkspeedMult = 1.00,
			startpos = v3(0,0,0),
			special = false,
		},

		["static"] = {
			botImg = "rbxassetid://13928716599",
			imgAnimated = false,

			botTheme = "rbxassetid://8028069841",
			themeDist = 0.25,
			botJSSFX = "rbxassetid://7188420724",

			botWalkspeed = 98,
			botJumpPower = 70,
			
			bloodmoonWalkspeedMult = 1.05,
			startpos = v3(0,0,0),
			special = false,
		},
		
		["flashbang"] = {
			botImg = "rbxassetid://10753198321",
			imgAnimated = false,

			botTheme = "rbxassetid://2552417180",
			themeDist = 0.25,
			botJSSFX = "rbxassetid://7970264045",

			botWalkspeed = 98,
			botJumpPower = 70,

			bloodmoonWalkspeedMult = 1.05,
			startpos = v3(0,0,0),
			special = false,
		},
	},

	exampleEnum = {
		data2={123}
	},
}

return nbEnum

3 Likes