· 6 years ago · Dec 29, 2019, 09:38 PM
1local point1 = script.Parent.Point1
2local point2 = script.Parent.Point2
3
4--Hide points
5point1.Transparency = 1
6point2.Transparency = 1
7
8--Generate region based off our 2 points.
9local region = Region3.new(point1.Position,point2.Position)
10
11--Create forcefield effect so players easily know the status of the hill
12local regionVisual = Instance.new("Part")
13regionVisual.Anchored = true
14regionVisual.CanCollide = false
15regionVisual.Transparency = 1
16regionVisual.Material = Enum.Material.ForceField
17regionVisual.Size = region.Size
18regionVisual.TopSurface = Enum.SurfaceType.Smooth
19regionVisual.BottomSurface = Enum.SurfaceType.Smooth
20regionVisual.Parent = script.Parent
21regionVisual.CFrame = region.CFrame
22
23--Parts to ignore when checking for parts in the region
24local ignoreList = {point1,point2,regionVisual}
25--Track which players are currently in the region
26local playerInBounds = {}
27--Easily call tween service
28local tween = game:GetService("TweenService")
29
30--Find if there are any alive players in the hill
31function checkForNewPlayers()
32 --Loop through each part in the region to see if it is a part of a player's charadcter
33 for _,part in ipairs(partsInRegion) do
34 --Check if part is a piece of a player's character
35 local player = game.Players:GetPlayerFromCharacter(part.Parent)
36 if player then
37 --Check playerInBounds table to see if player already exists
38 if not table.find(playerInBounds,player) then
39 --Check to make sure that player is alive
40 if player.Character.Humanoid.Health > 0 then
41 --Add alive player to the playerInBounds table
42 table.insert(playerInBounds,player)
43 end
44 end
45 end
46 end
47end
48
49--Remove players that are dead or no longer in the region
50function removeOldPlayers()
51 --Loop through each player in playerInBounds
52 for playerIndex,player in ipairs(playerInBounds) do
53 --Loop through each part in partsInRegion
54 for partIndex,part in ipairs(partsInRegion) do
55 --Check to see part is a piece of the player's charater
56 if player == game.Players:GetPlayerFromCharacter(part.Parent) then
57 --Only keep player in the table if they are alive
58 if player.Character.Humanoid.Health > 0 then
59 break
60 else
61 --Remove player if their health is less than 0
62 table.remove(playerInBounds,playerIndex)
63 --Break out of the loop after we have identified the player
64 break
65 end
66 --If we loop through all parts and don't find a part of player's character
67 elseif partIndex == #partsInRegion then
68 --Remove player if no character parts are in the region
69 table.remove(playerInBounds,playerIndex)
70 end
71 end
72 end
73end
74
75--Give points to players
76function addPoints()
77 --Check if there is one player in the region
78 if #playerInBounds == 1 then
79 --Increase player's point value
80 local points = playerInBounds[1].leaderstats.Points
81 points.Value = points.Value + 1
82 end
83end
84
85--Give nice pulse effect to display the hill's status
86function regionVisualEffect()
87 --If 1 player is in the hill pulse green
88 if #playerInBounds == 1 then
89 regionVisual.Transparency = 0
90 regionVisual.Color = Color3.new(0,1,0)
91 tween:Create(regionVisual,TweenInfo.new(1),{Transparency = 1}):Play()
92 --If more than 1 player pulse red
93 elseif #playerInBounds > 1 then
94 regionVisual.Transparency = 0
95 regionVisual.Color = Color3.new(1,0,0)
96 tween:Create(regionVisual,TweenInfo.new(1),{Transparency = 1}):Play()
97 end
98end
99
100--Run all of our functions every second
101while wait(1) do
102 --Find all parts in our region excluding the parts in our ignore list
103 partsInRegion = workspace:FindPartsInRegion3WithIgnoreList(region,ignoreList,math.huge)
104 removeOldPlayers()
105 checkForNewPlayers()
106 addPoints()
107 regionVisualEffect()
108end