· 5 years ago · Apr 09, 2020, 11:14 PM
1--[[
2As the function's name says, FindAvailableSpot, for the player and the person attempting to join from the invitation for the queue / party ( like in Fortnite )
3This of course is not my cleanest work, I made it in an hour for a DevForum post to help someone.
4The Parties is a variable for the parties available
5It then looks for the player's name + the strings 's party as that is how the folder's name is usually set up
6If the folder exists ( party ) and is not nil or null then it does a for loop through the party's children which are values
7It looks for an available spot, in order
8All available spots should have their value set to "None"
9Unavailable spots are usually claimed by other players who are in the party
10]]
11
12function FindAvailableSpot(player,joiner)
13 local Done = false
14 local Found = nil
15 if Parties:FindFirstChild(player.."'s party") ~= nil then
16 for i,v in pairs(Parties:FindFirstChild(player.."'s party").Party:GetChildren()) do
17 if v.Value == "None" and not Done then
18 Done = true
19 Found = i
20 end
21 if i == #Parties:FindFirstChild(player.."'s party").Party:GetChildren() and not Done then
22 Done = true
23 return "FULL"
24 elseif i == #Parties:FindFirstChild(player.."'s party").Party:GetChildren() and Done then
25 return Found
26 end
27 end
28 end
29end
30
31--[[
32RandomKeyMaker just makes a random key
33And then returns the random key, which then later gets fired to the client, it's just extra security measures
34This key usually ends up being used as a hash
35For a party invite
36So it inserts the key into a table with the players name and the player invited
37So then when the client(s) accept an invite with the key or the hash it would look through the table for the hash
38]]
39
40local function RandomKeyMaker()
41 local KeyTable = ""
42 for i,v in pairs(AvailableStrings) do
43 local Hello = math.random(1,2)
44 if Hello == 1 then
45 local RandomizedString = AvailableStrings[math.random(1,#AvailableStrings)]
46 KeyTable = KeyTable..RandomizedString
47 elseif Hello == 2 then
48 local RandomizedNumeral = math.random(1,100)
49 KeyTable = KeyTable..RandomizedNumeral
50 end
51 if i == #AvailableStrings then
52 return KeyTable
53 end
54 end
55end
56
57--[[
58This function basically just gets called with the player's name whenever a player leaves the server
59Basically completely deleting the party, so then they are not in a party that doesn't exist
60It disconnects them from it basically
61As the first function I explained does, it locates their party folder
62Goes through it, and makes the values to None
63Wait no
64It makes the party leader's value to none
65Before it does the for loop
66Because it can't fire a non-existent player's UserId
67So then it looks through it and makes sure the values are not none
68if the value is not none it'll get the player by their userid
69since that is what the value stores
70the player's userids who are in the party
71it then checks if the player is not nil or null , makes sure they exist
72and it fires their client to delete all other players who are in the party
73]]
74
75local function ClearOriginalParty(player)
76 if Parties:FindFirstChild(player.UserId.."'s party") ~= nil then
77 Parties:FindFirstChild(player.UserId.."'s party").Party["1"].Value = "None"
78 for i,v in pairs(Parties:FindFirstChild(player.UserId.."'s party").Party:GetChildren()) do
79 if v.Value ~= "None" then
80 local Player = Players:GetPlayerByUserId(tonumber(v.Value))
81 if Player ~= nil then
82 Events:FireClient(Player,"ENDPARTY")
83 if Parties:FindFirstChild(Player.UserId.."'s party") ~= nil then
84 if Parties:FindFirstChild(Player.UserId.."'s party").Party["1"].Value == "None" then
85 Parties:FindFirstChild(Player.UserId.."'s party").Party["1"].Value = Player.UserId
86 end
87 end
88 end
89 end
90 end
91 end
92end
93
94--[[
95The last function in the server
96Is self-explainable
97It locates the player by their user Id
98And sees what party they're in
99If and once they find the player
100It returns the player's party they're in
101]]
102
103local function LocatePlayer(player)
104 local Done = false
105 for i,v in pairs(Parties:GetChildren()) do
106 for _,Obj in pairs(v.Party:GetChildren()) do
107 if tonumber(Obj.Value) == player.UserId and not Done then
108 Done = true
109 return Obj
110 end
111 end
112 end
113end
114
115--[[
116That function just finds the object in the table by the index
117]]
118
119local function FindInTable(index,tbl)
120 for i,v in pairs(tbl) do
121 if i == index then
122 return v
123 end
124 end
125end
126
127--[[
128this one checks for players they missed
129This means for players who joined before them
130So it'll create an invite button for their name
131To make sure that everything functions
132As it only creates an invite button for their name when the PlayerAdded event is called
133That function was a MUST to ensure that players who joined before the client would be added
134As the PlayerAdded event will not be called unless a new player is being added into the server
135So then it would basically miss players
136That's the only reason why that function exists
137]]
138
139local function CheckForPlayersYouMissed()
140 for i,player in pairs(Players:GetPlayers()) do
141 if Invite:FindFirstChild(player.UserId) == nil and player ~= Player then
142 local Template = UI.InviteTemplate:Clone()
143 Template.Parent = Invite
144 Template.Name = player.UserId
145 Template.Text = player.Name
146 Template.MouseButton1Click:Connect(function()
147 if not InviteCoolDown then
148 InviteCoolDown = not InviteCoolDown
149 Events:FireServer("Invite",{tonumber(Template.Name)}) -- tell the server they want to invite the player
150 wait(4)
151 InviteCoolDown = not InviteCoolDown
152 end
153 end)
154 end
155 end
156end