Trying to make an NPC spawn based on team player count, despawn if there are no team players

I am trying to make an NPC spawn based on player count within a team. If there are no players within a specific team, the NPC gets put into ServerStorage until there is at least 1 player on the team.

Now I have fiddled with the value and it works as it should when there are no players the team. However, when I get on said team, the NPC does not despawn and put into ServerStorage. No errors within the output so I assume I am setting the values for the count wrong or something. At least it works somewhat but not how I need it.

game.Players.PlayerAdded:Connect(function(player)
	local msCount = 0
	local NPC = game.ServerStorage.NPC
	for i,v in pairs(game.Players:GetChildren()) do
		if v.Team == game.Teams["Medical Staff"] then
			msCount = msCount + 1
		end
	end
	if msCount == 0 then
		NPC.Parent = game.Workspace
	else 
		NPC.Parent = game.ServerStorage
	end
end)

Code only runs once when the player joins the game, You also need to check if a player is added or removed from a team and most likely as well check if there’s at least one player, which I think would allow you to get rid of checking if a player joins the game since your code only cares for players in a team

Not sure if this is what you mean but I did do an Update() function previously to check the number of players within the team and still no luck, then again this might also be wrong as well.

local Players = game:GetService("Players")
local ms_Team = game.Teams["Medical Staff"]:GetPlayers()
local NPC = game.ServerStorage.NPC

local function spawnNPC()
	NPC.Parent = workspace
end

local function removeNPC()
	workspace:WaitForChild("NPC").Parent = game.ServerStorage
end

local function update()
	local msCount = #ms_Team
	while msCount <= 0 do
		spawnNPC()
		task.wait()
	end
	while msCount > 0 do
		removeNPC()
	end
end

Not exactly there, When a person is added to the team (Team.PlayerAdded), parent the npc to ServerStorage. When a person is removed from the team (Team.PlayerRemoved), check if there are no players (#Team:GetPlayers() == 0), if there isn’t, parent the npc back to workspace

Right, it does work with the spawning/despawning now with the remade code as you suggested below, but I am still not understanding how to make it check if theres 1 player on the team so that when there isnt any players on the team it spawns in, when there is a player on the team it despawns.

local NPC = game.ServerStorage.NPC

game:GetService("Teams")["Medical Staff"].PlayerAdded:Connect(function()
	NPC.Parent = game.ServerStorage
	print("NPC is off-duty.")
end)

game:GetService("Teams")["Medical Staff"].PlayerRemoved:Connect(function()
	NPC.Parent = game.Workspace
	print("NPC is on-duty.")
end)

With #Team:GetPlayers() you are able to know how many people are inside the team.

local MedicalStaff = game:GetService("Teams")["Medical Staff"]

MedicalStaff.PlayerRemoved:Connect(function() -- If a player leaves the team
	if #MedicalStaff:GetPlayers() == 0 then -- If there is 0 players inside the team
		NPC.Parent = game.workspace
	end
end)

MedicalStaff.PlayerAdded:Connect(function() -- If a player enters the team
	NPC.Parent = game.ServerStorage -- Logically there is a player inside the team
end)

1 Like

Still not working properly. Now when I implemented this, it doesn’t work altogether now.

Hello, I’ve just tested it in Roblox Studio and it seems to work completly fine for me:

2d95ba6aee43e1feabcf92cbf1afa89d

On the video I change the player’s team between ‘Medical Staff’ and ‘Team’ (A random team I created) and everytime the player leaves the Medical Staff team the part appears, which would be the NPC.

Is there no errors or warnings on the console? Did you try debugging the code?

Ok, so I looked into it. Apparently it only works if NPC.Parent = game.Workspace is under PlayerRemoved and NPC.Parent = game.ServerStorage is under PlayerAdded (which is the opposite way of how it should be). So something is conflicting it when it’s the other way around the way I want it to be. Nothing under the console either. I’m not sure how to fix this so it’s correct.

Oh well, seems like I’m unable to help you anymore with the information you’ve given.
It makes no sense to me the fact that switching those 2 lines makes it work since both connections seem to be working fine. And the fact that there is no warnings or errors in the console is very weird also.

I guess It’s up to you now to check for anything conflicting with it, like I said the code works completly fine on Roblox studio for me, hopefully someone here knows anything about this.

Yeah I understand. I’ve also tried experimenting and seeing if moving the NPC directly into workspace then doing Destroy() if there is a player on the team but no luck either.

There are a couple of things I would like to point out.

  • table.getn() would be the proper way to get th number of items in a Table whilst #Table is more of a shortcut, this is known as “syntactic sugar” where the Programming language is designed to make it easier for you to code or express.

  • game.Players:GetPlayers() is the proper function rather than game.Players:GetChildren(), Unlike game.Players:GetChildren(), :GetPlayers() will automatically Assume the Player Instance so it would make it easier for you to work with Player related stuff. game.Players:GetChildren() will only return as Instance and not Player, As its Assumed the Children of the Object are Instances

  • You don’t have to do game.Workspace, there is a Global known as workspace which acts as its own independent variable instead of relying on a DataModel to find it for you.

In response to your points:

  • This isn’t necessarily the issue. The issue is the code that Isaque provided somewhat works, but it’s opposite of how I need it to work. And when I try to switch to how I need it to work, it doesn’t work at all.
  • This change has already been made to the code, still does not solve the issue.
  • Either with game.Workspace or workspace, code still does not function properly. Still does not solve the issue.

At this point, it seems no matter how I try to experiment with it to see if it works, it doesn’t function. There isn’t even errors or flags within Output/Console. So there is a piece of code missing, which I can’t understand or wrap my head around how, which leads me to believe the code isn’t connecting to the NPC being dragged from workspace back to ServerStorage or vice versa.

I never said it was the issue, very far from that, All i said was “A few things to point out” which should have made my point clear.

I guess not.

Well I’m just kinda lost and trying to wrap my head around what could be causing the code not to work one way but work a completely different way than how I intended. I considered what you wrote and pointed out. While those points should be noted, still haven’t found a solution yet.

I tried out the code Isaque provided and it works for me as well. There might either be a conflict with another script or something is up with the NPC itself? Are there any scripts that could be a culprit? Could the position of where you put the NPC make it difficult to tell if it’s working or not?

Update:

After messing around with the code Isaque provided, and after more numerous experiment attempts it seems that we’ve managed to narrow down the issue to lines 5 and 10. The code finally works.
Apparently the final function should’ve been:

For players leaving the team and if there are 0 players

game.ServerStorage.NPC.Parent = workspace

For players joining the team

workspace.NPC.Parent = game.ServerStorage

But since Isaque helped, i’ll mark it as the solution.

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