· 6 years ago · Oct 08, 2019, 11:42 PM
1--the part which represents our area the player can be in
2boundsPart = game.Workspace.Part
3
4--we need to create a touch event for part to be able to use getTouchingParts() since
5--if you look on the wiki, getTouchingParts does not work with cancollide unless it has a touch event connecting it
6boundsPart.Touched:Connect(function() end)
7
8--check if an item is in the table
9function isInTable(_table, item)
10 --loop through the items in the table
11 for i = 1, #_table do
12 --check if the item in the table is our item we're looking for
13 if(_table[i] == item)then
14 return true -- we found the item in the table return true
15 end
16 end
17
18 --we didn't find the item in the table so return false
19 return false
20end
21
22-----------------------------------------------------------------------------
23--testing our isInTable function just to make sure it works. This should be true, and then false
24--because "test"" is in table, but "not in table isnt".
25--you can delete this bit if you'd like
26myTable = {"test"}
27
28print(isInTable(myTable, "test"))
29print(isInTable(myTable, "not in table :("))
30-----------------------------------------------------------------------------
31
32--run the following code inside this while loop every 1 second
33while(wait(1))do
34 --this is a table that we will use for keeping track of who's in the area when we check using :GetTouchingParts
35 --every time the loop runs, playersInArea starts off as empty.
36 local playersInArea = {}
37
38 --get the parts that are touching the part
39 --[[
40 table looks something like this:
41 touchingParts = {
42 game.Workspace.Part2,
43 game.Workspace.royaltoe.Torso,
44 game.Workspace.royaltoe.Head
45 and so on(...)
46 }
47 ]]
48 local touchingParts = boundsPart:GetTouchingParts()
49
50 --loop through the touching parts table that we got when we used :GetTouchingParts()
51 for i = 1, #touchingParts do
52 --we want to see if the part being touched's parent is a character, and every character has a player in game.Players
53 --assuming they're not an NPC that is...
54
55 --so for the code below, we're going into the game.Players and looking for the player
56 --if it finds a child (the player) with the same name as the string 'touchingParts[i].Parent.Name'
57 --it returns the player object inside game.Players
58 --if it doesnt, then player will be nil
59 --this is why i was telling you about how if statements only run if the thing in the if statement is not false or nil
60 local player = game.Players:FindFirstChild(touchingParts[i].Parent.Name)
61
62 --if the player exists (if it is not nil), then do the following code in the if statement
63 if(player)then
64 --check if the player isn't already in the playersInArea table using the function we created, 'isInTable'
65 --the function returns a true or false value based on if the player is in the table which is why we have it in an if statement
66 --the reason we're checking is because we will find the player multiple times because multiple parts of theirs are in the boundsPart
67 if(not isInTable(playersInArea, player))then
68 --insert the player into our table 'playersInArea'
69 --i am passing the player object from game.Players in here, not the player's character.
70 --you may pass in the character if you'd like instead
71 table.insert(playersInArea, player)
72 end
73 end
74 end
75
76 --after the loop, check if only one player is in the bounds by checking how many players are in playersInArea table
77 if(#playersInArea >= 2)then
78 print("more than two")
79 elseif(#playersInArea == 1)then
80 print("one player")
81 else
82 print("no players")
83 end
84
85end