Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do i use a script to detect all players (the characters actually) touching a brick?

Kind of like Notoriety, it waits until ALL the characters are touching the Van, i want thta too, please help, thanks,

I tried

--when a player joins the value inside script increments
script.Parent.Touched:Connect(function(c)
if c.Parent:FindFirstChild("Masked") then
--Masked is value only inside player's characters...
if script.Value.Value < characters then
increments*
whatever

see? it takes too long, is there an easier way? just one script that keeps waiting then prints something when all characters r on brick? thanks :)

1 comment

0

i dont want any values included plz

2 answers

Solution

First, we will need to create a table to compare to the amount of players in the game (using # before a table will return the amount of values inside of it)

You can use the .Touched and .TouchEnded events of parts to detect when a player is touching a part and when they stop touching a part, we will use these to add and remove a player from the table.

It is also useful to use a debounce to make sure that the touched event is not spammed fruitlessly.

Revised Server Script

local array = {}
local debounce = true
script.Parent.Touched:Connect(function(c)
	if debounce == false then return end
	debounce = false
	if c.Parent:FindFirstChild("Masked") then
		local check = false
		for a, b in pairs(array) do
			if b == c.Parent.Name then
				check = true
			end
	    end
	    
		if check == false then
	    local count = #array + 1
	    table.insert(array, count, c.Parent.Name)
		    if #game:GetService("Players"):GetPlayers() == #array then
			    print("All Players Accounted For")
		    end
	    end
	    check = false
    end
    debounce = true
end)

script.Parent.TouchEnded:Connect(function(c)
    for d, e in pairs(array) do
	    if e == c.Parent.Name then
		    table.remove(array, d)
	    end
    end
end)

Explanation

The first block of code under the .Touched event checks the player's name against the tables in the value, if they are already in the table, then it will not allow the print to occur (we want to make sure that no extra values of the player's name are added to the table so it will perfectly match the amount of players)

The second block of code, assuming the check is successfully passed as false adds a player's name to the table using table.insert() and then checks the amount of instances in the Players service against the amount of values in the array table, and prints a statement if they match

The third block of code uses the .TouchEnded event to remove players from the table when they stop touching the part using table.remove()

So first we want a character limit and a character counter so we're going to define this.

local NeededCharacters = 10
local Characters = 0

Now that we know how much characters we need let's continue.

script.Parent.Touched:Connect(function(c)
  if c.Parent:FindFirstChild("Masked") ~= nil then

Here since we know that the players are have something named "Masked" inside of them so we are going to make sure it's not nil and it exists. Therefore something like a dropped tool or an unanchored part cant activate the script.

script.Parent.Touched:Connect(function(c)
  if c.Parent:FindFirstChild("Masked") ~= nil then
      Characters = Characters+1
      print("A player has touched the van")

Here we make the current amount of characters in the van raise by 1 everytime a player touches the van.

script.Parent.Touched:Connect(function(c)
  if c.Parent:FindFirstChild("Masked") ~= nil then
      Characters = Characters+1
      print("A player has touched the van")
      if Characters == NeededCharacters then
      print("Van is full!")

Here we can check if the van has been filled up yet to the maximum amount of players which is 10

local PlayerTouched = false

   if PlayerTouched == true then end
   PlayerTouched = true

Here we add a debounce to stop a single player from activating this script multiple times.

function PlayerEntered(c)

   script.Parent.Touched:Connect(PlayerEntered)

I like naming my functions allowing them to be much more easy to get.

Now we have.

local NeededCharacters = 10
local Characters = 0
local PlayerTouched = false


function PlayerEntered(c)
  if c.Parent:FindFirstChild("Masked") ~= nil then
      if PlayerTouched == true then return end
      PlayerTouched = true
      Characters = Characters+1
      print("A player has touched the van")
      if Characters == NeededCharacters then
      print("Van is full!")
      end
   end
end)

script.Parent.Touched:Connect(PlayerEntered)

Now we make a function to remove players that left the van

local PlayerLeft = false

function PlayerLeft(c)
  if c.Parent:FindFirstChild("Masked") ~= nil then
      if PlayerLeft == true then return end
      PlayerLeft = true
      Characters = Characters-1
      print("A player has left the van")
      end
   end
end)

script.Parent.TouchEnded:Connect(PlayerLeft)

So now our final script should be.

local NeededCharacters = 10
local Characters = 0
local PlayerTouched = false
local PlayerLeft = false


function PlayerEntered(c)
  if c.Parent:FindFirstChild("Masked") ~= nil then
      if PlayerTouched == true then return end
      PlayerTouched = true
      Characters = Characters+1
      print("A player has touched the van")
      if Characters == NeededCharacters then
      print("Van is full!")
      end
   end
end)

function PlayerLeft(c)
  if c.Parent:FindFirstChild("Masked") ~= nil then
      if PlayerLeft == true then return end
      PlayerLeft = true
      Characters = Characters-1
      print("A player has left the van")
      end
   end
end)

script.Parent.Touched:Connect(PlayerEntered)
script.Parent.TouchEnded:Connect(PlayerLeft)

If it doesn't work please tell me what I did wrong! :D

3 comments

0

The other dude answered you question while I was almost done with mine, but it's alright I just answer questions so I can also learn from it.

0

ill try this out

0

is htere a way to detect constantly how many players there are in game.Players?

This discussion is preserved as it was. New answers, comments, and votes are closed.