Players required in a team

Hello everyone!

I wanted to know how I can do so that to rob a store it only comes out when there is a certain number of people in a team, for example 2 policemen to rob a store.
I am using ProximityPrompt for shop

3 Likes

Example code

local found = 0
for i,v in pairs(game.Players:GetPlayers()) do
	if v.Team == "Policemen" then
		found+=1
	end
end

if found >= 2 then
	--Rob bank!!!
end
3 Likes

Ok, would this part have to be outside the prompt function?

local found = 0
for i,v in pairs(game.Players:GetPlayers()) do
	if v.Team == "Policemen" then
		found+=1
	end
end

and the other if not?

2 Likes

Can you show me your prompt function?, this would most likely have to be inside.

3 Likes

@wf_sh script works but i would put it inside a function like this:

local function check()
	local found = 0
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Team == "Policemen" then
			found+=1
		end
	end
	return found
end

--your function
local Check = check()

if(Check >= 2) then
	--start rob
end
--end

3 Likes
local prompt = script.Parent
local found = 0

for i,v in pairs(game.Players:GetPlayers()) do
	if v.Team == "Policia" then
		found+=1
	end
end



prompt.Triggered:Connect(function(plr)
	if found >= 2 then
		plr.leaderstats.Dinero.Value = plr.leaderstats.Dinero.Value + math.random(35000, 50000)
		script.Parent.Style = "Custom"
		script.Parent.Enabled = false
		script.Parent.Parent.robo.Enabled = true

		wait(600)

		script.Parent.Enabled = true
		script.Parent.Style = "Default"
		script.Parent.Parent.robo.Enabled = true
	end
end)
2 Likes
local prompt = script.Parent

local function MeetsMinPlayers()
	local found = 0
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Team == "Policia" then
			found+=1
		end
	end
	
	return (found>=2)
end


prompt.Triggered:Connect(function(plr)
	if MeetsMinPlayers() then
		plr.leaderstats.Dinero.Value = plr.leaderstats.Dinero.Value + math.random(35000, 50000)
		script.Parent.Style = "Custom"
		script.Parent.Enabled = false
		script.Parent.Parent.robo.Enabled = true

		task.wait(600)

		script.Parent.Enabled = true
		script.Parent.Style = "Default"
		script.Parent.Parent.robo.Enabled = true
	end
end)
3 Likes

Instead of the for loop, you can do

local NumerOfPolice = #game:GetService("Teams").Policia:GetPlayers()

to get the number of players in the police team

5 Likes
local prompt = script.Parent

function MeetsMinPlayers()
	local NumerOfPolice = #game:GetService("Teams").Policia:GetPlayers()
	return (NumerOfPolice >= 2)
end


prompt.Triggered:Connect(function(plr)
	if MeetsMinPlayers() then
		plr.leaderstats.Dinero.Value += math.random(35000, 50000)
		script.Parent.Style = "Custom"
		script.Parent.Enabled = false
		script.Parent.Parent.robo.Enabled = true

		wait(600)

		script.Parent.Enabled = true
		script.Parent.Style = "Default"
		script.Parent.Parent.robo.Enabled = true
	end
end)
3 Likes

I think it doesn’t work, because when I finish interacting with the prompt it doesn’t give me the money

Any output errors? If so, show me

I already checked the output but there is nothing.

Are there atleast 2 people in the Policia team?

Show us the full output of this code

local prompt = script.Parent

function MeetsMinPlayers()
	local NumerOfPolice = #game:GetService("Teams").Policia:GetPlayers()
	return (NumerOfPolice >= 2)
end

prompt.Triggered:Connect(function(plr)
	if MeetsMinPlayers() then
        print("Giving cash\n" .. plr.leaderstats.Dinero.Value)
		plr.leaderstats.Dinero.Value += math.random(35000, 50000)
        print(plr.leaderstats.Dinero.Value)
		prompt.Style = "Custom"
		prompt.Enabled = false
		prompt.Parent.robo.Enabled = true

		task.wait(600)

        print("Re enabling prompt")
		prompt.Enabled = true
		prompt.Style = "Default"
		prompt.Parent.robo.Enabled = true
	end
end)

put that the minimum is 1 to be able to try it myself

I did the same thing to add prints but they don’t appear in the output either, i test

That is very odd… Can you try this:

function MeetsMinPlayers()
	local NumerOfPolice = #game:GetService("Teams").Policia:GetPlayers()
	print(NumerOfPolice .. " police on the team")
	return (NumerOfPolice >= 2)
end

I know what the error was, I changed the Players teams and they only changed my client

Oh alright, that makes sense. Next time don’t do that haha

yes haha, well thank you both for helping!