So I was working on a kill confirmed tag system for my game, and originally a bug occurred that gave the first person who joins the exp, not the person who collected the tag left by someone dying. I then edited the script a little, but another new issue happened, that being if someone were to collect a tag, everyone in the server would gain the exp.
Server script:
In ServerScriptService.
local DSS = game:GetService("DataStoreService")
local RankDS = DSS:GetDataStore("RankData")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local levelValue = Instance.new("IntValue")
levelValue.Name = "Rank"
levelValue.Parent = leaderstats
local expValue = Instance.new("NumberValue")
expValue.Name = "expValue"
expValue.Parent = plr
local maxExp = Instance.new("NumberValue")
maxExp.Name = "maxExp"
maxExp.Parent = plr
maxExp.Value = 100
local function LevelUp()
levelValue.Value += 1
expValue.Value = 0
maxExp.Value = (levelValue.Value + 1) * 100
end
local function GainExp(gain)
expValue.Value += gain
if expValue.Value > maxExp.Value or expValue.Value == maxExp.Value then
LevelUp()
end
end
local tag = game.ServerStorage.Tag:Clone()
local tagFolder = game.Workspace.Tags
local event = game:GetService("ReplicatedStorage").TagEvent
event.OnServerEvent:Connect(function(playyer,teamColor,hmoidP)
local tag = game.ServerStorage.Tag:Clone()
tag.Color = teamColor
tag.Position = hmoidP
tag.TagBillboardGui.TagLabel.TextColor3 = teamColor
tag.Parent = tagFolder
tag.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
for i,v in pairs(hit.Parent:GetDescendants()) do
if v:IsA("Humanoid") then
if player.TeamColor.Color == tag.Color then
if v.Health ~= 0 then
GainExp(5000)
tag:Destroy()
end
end
if player.TeamColor.Color ~= tag.Color then
if v.Health ~= 0 then
GainExp(5000)
tag:Destroy()
end
end
end
end
--if plr.Humanoid.Health ~= 0 then
-- plr.expValue += 100
-- tag:Destroy()
--end
end)
end)
--Load data
local PlrId = plr.UserId
print("First ID var has been set.")
local data
local data2 = nil
local data3 = nil
local success, errormessage = pcall(function()
data = RankDS:GetAsync(PlrId)
end)
print("First pcall has been fired.")
if success then
print("Success = true!")
levelValue.Value = data[1]
expValue.Value = data[2]
maxExp.Value = data[3]
end
end)
--Save data
game.Players.PlayerRemoving:Connect(function(plr)
local PlrId = plr.UserId
local data = plr.leaderstats.Rank.Value
local data2 = plr.expValue.Value
local data3 = plr.maxExp.Value
print("Second variables has been set!")
local success, errormessage = pcall(function()
RankDS:SetAsync(PlrId, {data,data2,data3})
end)
print("Second pcall has been fired!")
if success then
print("Data has been saved successfully")
else
print("There has been an error")
warn(errormessage)
end
end)
Local script:
In StarterCharacterScripts.
local event = game:GetService("ReplicatedStorage").TagEvent
local plr = game.Players.LocalPlayer
local char = plr.Character
local hmoid = char.Humanoid
hmoid.Died:Connect(function()
local hmoidP = char.HumanoidRootPart.Position
local teamColor = plr.TeamColor.Color
event:FireServer(teamColor,hmoidP)
end)
Any help would be appreciated.