Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views5 pages

Local Teams gameGetService (Teams)

The document outlines a Lua script for a game that manages player statistics, including kills, deaths, wins, experience points (XP), and levels. It includes functions for handling player respawns, tracking kills and deaths, awarding gems and coins based on wins, and saving player data using DataStores. The script also features animations for level-ups and ensures that only players in allowed teams can interact with certain game mechanics.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Local Teams gameGetService (Teams)

The document outlines a Lua script for a game that manages player statistics, including kills, deaths, wins, experience points (XP), and levels. It includes functions for handling player respawns, tracking kills and deaths, awarding gems and coins based on wins, and saving player data using DataStores. The script also features animations for level-ups and ensures that only players in allowed teams can interact with certain game mechanics.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

local Teams = game:GetService("Teams")

local DSS = game:GetService("DataStoreService")


local CoinsDS = DSS:GetDataStore("CoinsDS")
local HighestStreaksDS = DSS:GetDataStore("HighestStreaksDS")
local GemsDS = DSS:GetDataStore("GemsDS") -- ✅ Added for gem saving
local TweenService = game:GetService("TweenService")
local stands = {}

-- Function to handle player respawn


function onPlayerRespawn(player)
local character = player.Character
if character then
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.Died:Connect(function()
onHumanoidDied(humanoid, player)
end)
end
end
end

-- Handle humanoid death


function onHumanoidDied(humanoid, player)
local stats = player:FindFirstChild("leaderstats")
if stats then
local deaths = stats:FindFirstChild("W0s")
local strk = stats:FindFirstChild("Streak")
if deaths and strk then
deaths.Value = deaths.Value + 1
strk.Value = 0

local killer = getKillerOfHumanoidIfStillInGame(humanoid)


handleKillCount(humanoid, player, killer)
Send_DB_Event_Died(player, killer)
end
end
end

-- Get killer
function getKillerOfHumanoidIfStillInGame(humanoid)
local tag = humanoid:FindFirstChild("creator")
if tag and tag.Value then
local killer = tag.Value
if killer:IsA("Player") and killer.Parent then
return killer
end
end
return nil
end

-- Handle kill logic


function handleKillCount(humanoid, victim, killer)
if killer and killer ~= victim and isInAllowedTeam(killer) and
isInAllowedTeam(victim) then
local stats = killer:FindFirstChild("leaderstats")
if stats then
local kills = stats:FindFirstChild("K0s")
local strk = stats:FindFirstChild("Streak")
local xp = stats:FindFirstChild("XP")
local level = stats:FindFirstChild("Level")
local highStrk = stats:FindFirstChild("HighestStreak")

if kills and strk and xp and level then


kills.Value += 1
strk.Value += 0
xp.Value += (2.1 + (level.Value - 1) * 4.4)

if highStrk and strk.Value > highStrk.Value then


highStrk.Value = strk.Value
end

Send_DB_Event_Kill(killer, victim)
end

local coinStats = killer:FindFirstChild("Coin_Stats")


if coinStats then
local coins = coinStats:FindFirstChild("Coins")
if coins then
coins.Value += 1
end
end
end
end
end

-- Handle XP and level


function onXPChanged(player, XP, level)
if XP.Value >= level.Value * 100 then
XP.Value = 0
level.Value = level.Value + 1
onLevelUp(player, XP, level)
end
end

-- Level up animation
function onLevelUp(player, XP, level)
local gui = Instance.new("ScreenGui", player.PlayerGui)
local levelUpFrame = Instance.new("Frame", gui)
levelUpFrame.Size = UDim2.new(0.5, 0, 0.1, 0)
levelUpFrame.Position = UDim2.new(0.25, 0, 0.45, 0)
levelUpFrame.BackgroundColor3 = Color3.new(1, 1, 1)
levelUpFrame.BackgroundTransparency = 1

local levelUpMessage = Instance.new("TextLabel", levelUpFrame)


levelUpMessage.Size = UDim2.new(1, 0, 1, 0)
levelUpMessage.Text = player.Name .. " has leveled up!"
levelUpMessage.BackgroundTransparency = 1
levelUpMessage.Font = Enum.Font.FredokaOne
levelUpMessage.TextColor3 = Color3.new(1, 1, 1)
levelUpMessage.TextScaled = true

local sound = Instance.new("Sound", player.Character)


sound.SoundId = "rbxassetid://341542294"
sound.Volume = 2
sound:Play()

local tweenInfo = TweenInfo.new(2, Enum.EasingStyle.Bounce,


Enum.EasingDirection.Out)
local tween = TweenService:Create(levelUpFrame, tweenInfo, {Position =
UDim2.new(0.25, 0, 0.4, 0)})
tween:Play()

wait(3)
gui:Destroy()
end

-- Allowed teams
function isInAllowedTeam(player)
local team = player.Team
if team then
local teamName = team.Name
return teamName == "RED" or teamName == "BLUE" or teamName == "GREEN"
or teamName == "YELLOW"
end
return false
end

-- Handle win change (for Gems and Coins)


function onWinsChanged(player, wins)
local lastWinCount = wins.Value
wins:GetPropertyChangedSignal("Value"):Connect(function()
if wins.Value > lastWinCount then
local addedWins = wins.Value - lastWinCount

-- Give Gems
local gemStats = player:FindFirstChild("Gem_Stats")
if gemStats then
local gems = gemStats:FindFirstChild("Gems")
if gems then
local addedGems = addedWins * 5
gems.Value = gems.Value + addedGems
end
end

-- Give Coins
local coinStats = player:FindFirstChild("Coin_Stats")
if coinStats then
local coins = coinStats:FindFirstChild("Coins")
if coins then
local addedCoins = addedWins * 25 -- 💰 Give 25 Coins
per Win
coins.Value = coins.Value + addedCoins
end
end

lastWinCount = wins.Value
end
end)
end

-- Main entry for player setup


function onPlayerEntered(newPlayer)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"

local kills = Instance.new("IntValue")


kills.Name = "K0s"
kills.Value = 0
kills.Parent = stats

local deaths = Instance.new("IntValue")


deaths.Name = "W0s"
deaths.Value = 0
deaths.Parent = stats

local streak = Instance.new("IntValue")


streak.Name = "Streak"
streak.Value = 0
streak.Parent = stats

local wins = Instance.new("IntValue")


wins.Name = "Wins"
wins.Value = 0
wins.Parent = stats

local level = Instance.new("IntValue")


level.Name = "Level"
level.Value = 1
level.Parent = stats

local xp = Instance.new("IntValue")
xp.Name = "XP"
xp.Value = 0
xp.Parent = stats

local highestStreak = Instance.new("IntValue")


highestStreak.Name = "HighestStreak"
highestStreak.Value = 0
highestStreak.Parent = stats

-- Load highest streak


pcall(function()
local savedHighest = HighestStreaksDS:GetAsync(newPlayer.UserId)
if savedHighest then
highestStreak.Value = savedHighest
end
end)

stats.Parent = newPlayer

xp.Changed:Connect(function() onXPChanged(newPlayer, xp, level) end)


onWinsChanged(newPlayer, wins)

-- Coins
local coinFolder = Instance.new("Folder")
coinFolder.Name = "Coin_Stats"
coinFolder.Parent = newPlayer

local coins = Instance.new("IntValue")


coins.Name = "Coins"
coins.Value = 0
coins.Parent = coinFolder

local savedCoins = CoinsDS:GetAsync(newPlayer.UserId) or 0


coins.Value = savedCoins
coins.Changed:Connect(function()
CoinsDS:SetAsync(newPlayer.UserId, coins.Value)
end)

-- ✅ Gems
local gemFolder = Instance.new("Folder")
gemFolder.Name = "Gem_Stats"
gemFolder.Parent = newPlayer

local gems = Instance.new("IntValue")


gems.Name = "Gems"
gems.Value = 0
gems.Parent = gemFolder

local savedGems = GemsDS:GetAsync(newPlayer.UserId) or 0


gems.Value = savedGems

gems.Changed:Connect(function()
GemsDS:SetAsync(newPlayer.UserId, gems.Value)
end)

newPlayer.CharacterAdded:Connect(function()
onPlayerRespawn(newPlayer)
end)
end

-- Player connection
game.Players.PlayerAdded:Connect(function(newPlayer)
onPlayerEntered(newPlayer)
end)

-- Events
function Send_DB_Event_Died(victim, killer)
local killername = "unknown"
if killer then killername = killer.Name end
print(victim.Name, " was killed by ", killername)

if shared["deaths"] then
shared["deaths"](victim, killer)
print("Death event sent.")
end
end

function Send_DB_Event_Kill(killer, victim)


print(killer.Name, " killed ", victim.Name)
if shared["kills"] then
shared["kills"](killer, victim)
print("Kill event sent.")
end
end

You might also like