So I have Guns for for classes that are Team-Specific Colored. Is there any way to get the Player's Team?
Here's the LeaderBoard script I'm using (It's the Default Roblox-Made CTF one)
--------------------
--| WaitForChild |--
--------------------
-- Waits for parent.child to exist, then returns it
local function WaitForChild(parent, childName)
assert(parent, "ERROR: WaitForChild: parent is nil")
while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end
return parent[childName]
end
-----------------
--| Variables |--
-----------------
local RbxUtility = LoadLibrary('RbxUtility')
local PlayersService = Game:GetService('Players')
local Set = WaitForChild(script, 'Set')
local Apply = WaitForChild(script, 'Apply')
local GetPlayerScore = WaitForChild(script, 'GetPlayerScore')
local SetPlayerScore = WaitForChild(script, 'SetPlayerScore')
local IncreasePlayerScore = WaitForChild(script, 'IncreasePlayerScore')
local GetHighestScoringPlayer = WaitForChild(script, 'GetHighestScoringPlayer')
local GetSerialized = WaitForChild(script, 'GetSerialized')
local SetSerialized = WaitForChild(script, 'SetSerialized')
local MasterStatsTable = {}
-------------------------
--| Utility Functions |--
-------------------------
-- Gives player the stats in statsTable
local function SetOrCreatePlayerStats(player, statsTable)
local leaderstats = player:FindFirstChild('leaderstats')
if not leaderstats then
leaderstats = Instance.new('Model')
leaderstats.Name = 'leaderstats'
leaderstats.Parent = player
end
for stat, statValue in pairs(statsTable) do
local statName = statValue.Name or stat -- Fall back on the stat's index as its name if it has no Name property
local value = statValue.Value or 0
local currentStat = leaderstats:FindFirstChild(statName)
if not currentStat then
currentStat = Instance.new('IntValue')
currentStat.Name = statName
currentStat.Parent = leaderstats
end
currentStat.Value = value
end
end
-- Returns a table of player's stats
local function GetPlayerStats(player)
local statsTable = {}
local leaderstats = player:FindFirstChild('leaderstats')
if leaderstats then
for _, stat in pairs(leaderstats:GetChildren()) do
statsTable[stat.Name] = {Name = stat.Name, Value = stat.Value}
end
end
return statsTable
end
-- Returns player's value for score
local function GetPlayerScoreValue(player, score)
if not player or not score then print("ERROR: Empty parameter(s)") return end
local leaderstats = player:FindFirstChild('leaderstats')
if leaderstats then
local scoreObject = leaderstats:FindFirstChild(score)
if scoreObject then
return scoreObject.Value
end
end
return nil
end
-- Returns the player with userId == playerId
local function FindPlayerById(playerId)
if not playerId then return nil end
for _, player in pairs(PlayersService:GetPlayers()) do
if player.userId == playerId then
return player
end
end
return nil
end
--------------------------
--| Bindable Functions |--
--------------------------
-- Set: Sets MasterStatsTable to newStatsTable and gives all players the new stats
Set.OnInvoke = function(newStatsTable)
MasterStatsTable = newStatsTable
for _, player in pairs(PlayersService:GetPlayers()) do
while player:FindFirstChild('leaderstats') do
player.leaderstats:Destroy()
end
SetOrCreatePlayerStats(player, MasterStatsTable)
end
end
-- Apply: Applies MasterStatsTable to player
Apply.OnInvoke = function(player)
SetOrCreatePlayerStats(player, MasterStatsTable)
end
-- GetPlayerScore: [See GetPlayerScoreValue]
GetPlayerScore.OnInvoke = GetPlayerScoreValue
-- SetPlayerScore: Sets player's score to value
SetPlayerScore.OnInvoke = function(player, score, value)
if not player or not score or not value then print("ERROR: Empty parameter(s)") return end
local leaderstats = player:FindFirstChild('leaderstats')
if leaderstats then
local scoreObject = leaderstats:FindFirstChild(score)
if scoreObject then
scoreObject.Value = value
end
end
end
-- IncreasePlayerScore: Increases player's score by amount
IncreasePlayerScore.OnInvoke = function(player, score, amount)
if not player or not score or not amount then print("ERROR: Empty parameter(s)") return end
local leaderstats = player:FindFirstChild('leaderstats')
if leaderstats then
local scoreObject = leaderstats:FindFirstChild(score)
if scoreObject then
scoreObject.Value = scoreObject.Value + amount
end
end
end
-- GetHighestScoringPlayer: Returns the player with the highest value for score, along with the value
GetHighestScoringPlayer.OnInvoke = function(score)
if not score then print("ERROR: Empty parameter(s)") return end
local highestScoringPlayer = nil
local highestScore = 0
for _, player in pairs(PlayersService:GetPlayers()) do
local playerScore = GetPlayerScoreValue(player, score)
if playerScore then
if not highestScoringPlayer or playerScore > highestScore then
highestScoringPlayer = player
highestScore = playerScore
end
end
end
return highestScoringPlayer, highestScore
end
-- GetSerialized: Returns a serialized string of all players' stats
GetSerialized.OnInvoke = function()
local allLeaderStats = {}
for _, player in pairs(PlayersService:GetPlayers()) do
table.insert(allLeaderStats, player.userId, GetPlayerStats(player))
end
return RbxUtility:EncodeJSON(allLeaderStats)
end
-- SetSerialized: Takes a serialized string of all players' stats and applies the stats to all players
SetSerialized.OnInvoke = function(statsString)
if not statsString then print("ERROR: Empty parameter") return end
local newLeaderStats = RbxUtility:DecodeJSON(statsString)
if newLeaderStats then
for playerId, statsTable in pairs(newLeaderStats) do
local player = FindPlayerById(tonumber(playerId))
if player then
SetOrCreatePlayerStats(player, statsTable)
else
print("Player with userId =", playerId, "no longer exists in the server.")
end
end
end
end
