Why leaderstats sometimes working, and sometimes no ( especcialy kills)

Here is script leaderstats:

local PlayerService = game:GetService("Players")

PlayerService.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local deaths = Instance.new("IntValue")
	deaths.Name = "Deaths"
	deaths.Parent = leaderstats

	local kills = Instance.new("IntValue")
	kills.Name = "Kills"
	kills.Parent = leaderstats

	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")

		-- Checking for death
		humanoid.Died:Connect(function()
			print("bomb")
			-- Если персонаж уже мертв, не выполнять повторно обработку
			if humanoid.Health <= 0 then
				-- Увеличиваем количество смертей
				deaths.Value += 1
				local tag = humanoid:FindFirstChild("creator")
				print(tag, "tag") -- added
				if tag and tag.Value then
					local killer = tag.Value
					print(killer, "tag.value") -- added
					-- Увеличиваем количество убийств у убийцы
					if killer and killer:IsA("Player") then
						local killerStats = killer:FindFirstChild("leaderstats")
						print(killerStats, "killerstats") -- added
						if killerStats then
							local killerKills = killerStats:FindFirstChild("Kills")
							print(killerKills, "killerkills")  -- added
							if killerKills then
								killerKills.Value += 1
								warn(killer.Name, "Killed", player.Name)
							end
						end
					end
				end

				-- Удаляем объект "creator" после смерти
				if tag then
					tag:Destroy()
				end
			end
		end)
	end)
end)

1 Like

what is error,

what’s the behaviour you want

2 Likes

Kills ar not counting ( sometimes counting) I can add you on studio, and you can check

2 Likes

Is the tag value an object value or string value?

1 Like

Maybe issue on tools, can I add you on studio?

1 Like

Can not join studio right now. Can you show us the tag value and output when testing.

1 Like

Here is video https://youtu.be/KLp0DyA7ZpU

1 Like

By the video, it seemed like the player 2 got the kills

I don’t see anything inherently wrong with the code you’ve provided us. This suggests that the weapons you use to kill players weren’t properly designed to interact with your creator-tag system

1 Like

Check the tool script to make sure it is creating the tag if there is not tag since you destroy the tag.

Yea, that maybe issue with weapons. But I can’t find it

I’ll send the scripts of weapon later

In the studio output, you should be able to press the error and it will take you to the line

there might be an error in the script not connected to the kills but as long as there is an error any code after the error wont work

Okay guys, i’ll send the sctipts of weapon later okay?

1 Like

theres prob an error in the script above i didnt read it

If you are testing this in studio only, then there are certain cases where your local server closes before your data can be saved.

Does your leaderstats not save sometimes in the actual Roblox client too?

It is also recommended to look into periodically auto-saving data as well as using game:BindToClose() to avoid potential data loss as it keeps the server open for 30 seconds to allow for your player data saving scripts to fully run and finish. (The BindToClose method will not run as expectedly while in Studio though)

1 Like

Later, i’ll send a scripts of weapon

1 Like

Guys, here are a scripts:

just script:

char = script.Parent.Parent.Parent.Character
joints = {}
glues = {}
humanoid = nil

function onSelected(mouse)
	local torso = char:findFirstChild("Torso")
	if torso == nil then return end
	local c = torso:GetChildren()
	for i = 1, #c do
		if c[i]:IsA("JointInstance") then
			table.insert(joints,c[i])
			local glue = Instance.new("Rotate")
			glue.C0 = c[i].C0
			glue.C1 = c[i].C1
			glue.Part0 = c[i].Part0
			glue.Part1 = c[i].Part1
			glue.Name = c[i].Name
			table.insert(glues,glue)
			c[i].Part1.CanCollide = true
			c[i].Parent = nil
			glue.Parent = torso
		end
	end
	local k = char:GetChildren()
	for i = 1,#k do
		if k[i].className == "Hat" then
			if k[i]:findFirstChild("Handle") ~= nil then
				local weld = Instance.new("Weld")
				weld.Name = "Weld"
				weld.Part0 = char.Head
				weld.Part1 = k[i].Handle
				weld.C0 = char.Head.CFrame:inverse()
				weld.C1 = k[i].Handle.CFrame:inverse()
				weld.Parent = k[i].Handle
			end
		end
	end
	humanoid = char:findFirstChild("Humanoid")
	if humanoid ~= nil then humanoid.Parent = nil end
end

function onDeselected(mouse)
	for i = 1, #glues do
		glues[i]:remove()
	end
	for i = 1, #joints do
		joints[i].Parent = char:findFirstChild("Torso")
	end
	--[[local k = char:GetChildren()
	for i = 1,#k do
		if k[i].className == "Hat" then
			if k[i]:findFirstChild("Handle") ~= nil then
				k[i].Handle.Weld:remove()
			end
		end
	end]]
	if humanoid ~= nil then humanoid.Parent = char end
end

--script.Parent.Selected:connect(onSelected)
--script.Parent.Deselected:connect(onDeselected)

script.Parent.Equipped:Connect(onSelected)
script.Parent.Unequipped:Connect(onDeselected)

server(name of script)

local punch = script.Parent
local remote = punch.Remote
local hitpart = punch.HitPart
local humanoidlist = {}
local touch
local debris = game:GetService("Debris")
local char
local hrp

function CheckHumanoid(hum)
	for i, v in pairs(humanoidlist) do
		if humanoidlist[i] == hum then
			return true
		end
	end
	return false
end

function Velocity(x, y, z, maxtorque, maxforce, vector, hit)
	local attachment = Instance.new("Attachment", hit)
	local linear = Instance.new("LinearVelocity", hit)
	linear.MaxForce = maxforce
	linear.Attachment0 = attachment
	linear.RelativeTo = Enum.ActuatorRelativeTo.World
	linear.VectorVelocity = vector

	local angular = Instance.new("AngularVelocity", hit)
	angular.MaxTorque = maxtorque
	angular.AngularVelocity = Vector3.new(x, y, z)
	angular.Attachment0 = attachment
	angular.RelativeTo = Enum.ActuatorRelativeTo.Attachment0

	debris:AddItem(angular, 0.5)
	debris:AddItem(linear, 0.5)
	debris:AddItem(attachment, 0.5)
end

local function initialize()
	punch.Enabled = true
	char = punch.Parent
	hrp = char:FindFirstChild("HumanoidRootPart")

	local weld = hitpart:FindFirstChild("Weld")
	if weld then
		weld.Part0 = char:FindFirstChild("Right Arm")
		weld.Part1 = hitpart
	end

	local humanoid = char:FindFirstChildOfClass("Humanoid")
	if humanoid then
		humanoid.Died:Connect(function()
			punch.Enabled = false
			if touch then
				touch:Disconnect()
				touch = nil
			end
		end)
	end
end

function cooldown()
	coroutine.wrap(function()
		punch.Enabled = false
		local cooldownTime = 10 -- Установка времени отката на 10 секунд
		local interval = 0.1
		local steps = cooldownTime / interval

		for i = 1, steps do
			punch.Name = "[" .. string.format("%.1f", cooldownTime - (i * interval)) .. "]"
			wait(interval)
		end

		punch.Name = "Haymaker"
		punch.Enabled = true
	end)()
end

function ApplyRagdoll(character)
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if humanoid and humanoid.Health > 0 then
		humanoid:ChangeState(Enum.HumanoidStateType.Physics)
	end

	for _, part in pairs(character:GetChildren()) do
		if part:IsA("BasePart") and part ~= hrp then
			part:SetNetworkOwner(nil)
			if part:FindFirstChildOfClass("Motor6D") then
				local constraint = Instance.new("BallSocketConstraint")
				constraint.Parent = part
				constraint.Attachment0 = Instance.new("Attachment", part)
				constraint.Attachment1 = Instance.new("Attachment", part.Parent:FindFirstChild(part.Name))
				debris:AddItem(constraint, 2)
			end
		end
	end

	wait(2)

	if humanoid and humanoid.Health > 0 then
		humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
	end

	for _, part in pairs(character:GetChildren()) do
		if part:IsA("BasePart") and part ~= hrp then
			part:SetNetworkOwner(game.Players:GetPlayerFromCharacter(character))
		end
	end
end

remote.OnServerInvoke = function(plr, action)
	if action == "Swing" then
		if punch.Enabled == true then
			punch.Enabled = false

			delay(0.2, function()
				touch = hitpart.Touched:Connect(function(hit)
					if hit and hit.Parent then
						local hum = hit.Parent:FindFirstChildOfClass("Humanoid")
						if hum then
							local check = CheckHumanoid(hum)
							local otherhrp = hit.Parent:FindFirstChild("HumanoidRootPart")
							if otherhrp and hrp then
								local delta = otherhrp.Position - hrp.Position
								if not check then
									table.insert(humanoidlist, hum)
									Velocity(0, 0, 25, 40, 9999999, delta * 20, otherhrp)  -- Увеличен импульс
									hum:TakeDamage(10)
									hitpart.Hit:Play()
									ApplyRagdoll(hit.Parent)

									-- Add creator tag
									local creatorTag = Instance.new("ObjectValue")
									creatorTag.Name = "creator"
									creatorTag.Value = plr
									creatorTag.Parent = hum
									debris:AddItem(creatorTag, 2)  -- Adjust duration as needed

									wait(1)
									humanoidlist = {}
								end
							end
						end
					end
				end)
			end)

			delay(1, function()
				if touch then
					touch:Disconnect()
					touch = nil
				end
			end)

			cooldown()
			return true
		end
	end
end

punch.Equipped:Connect(initialize)

punch.SwingSound.OnServerEvent:Connect(function(plr)
	hitpart.Swing:Play()
end)

tool handler

local tool = script.Parent
local debounce = true
tool.Activated:Connect(function()
	if debounce == true then
		debounce = false
		wait(tool.Cooldown.Value)
		debounce = true
	end
end)

tool.HitPart.Touched:Connect(function(hit) -- Ragdoll
	if debounce == false then
		script.Disabled = true
		local Ragdoll = require(game.ServerScriptService.Module)
		Ragdoll.Start(hit.Parent)
		local force = Instance.new("BodyVelocity", hit.Parent.HumanoidRootPart)
		force.MaxForce = Vector3.new(2,2,2) * math.huge
		local direction = (hit.Parent.HumanoidRootPart.CFrame.Position - tool.Parent.HumanoidRootPart.CFrame.Position).Unit
		force.Velocity = (direction + Vector3.new(0,1,0)).Unit * 25
		local rotation = Instance.new("BodyAngularVelocity", hit.Parent.HumanoidRootPart)
		rotation.AngularVelocity = Vector3.new(1,1,1) * math.pi * 5
		rotation.MaxTorque = Vector3.new(2,2,2) * math.huge
		rotation.P = 5000
		tool.Handle.Slap:Play()
		wait(0.35)
		force:Destroy()
		rotation:Destroy()
		script.Disabled = false
		wait(math.random(1.25, 3.15))
		Ragdoll.Stop(hit.Parent)
	end
end)

client

local punch = script.Parent
local hitpart = punch.HitPart
local anim = punch:WaitForChild("Animations")
local remote = punch:WaitForChild("Remote")
local char = game.Players.LocalPlayer.Character
local lhum = char:FindFirstChildOfClass("Humanoid")
local loadswing = lhum:LoadAnimation(anim:WaitForChild("Swing1"))
local loadidle = lhum:LoadAnimation(anim:WaitForChild("Idle"))
local sound = punch:WaitForChild("SwingSound")
local equipped = false

punch.Equipped:Connect(function(mouse)

	equipped = true
	loadidle:Play()

	punch.Unequipped:Connect(function()

		equipped = false

		if loadswing then

			loadswing:Stop()

		end

		loadidle:Stop()

	end)

end)

punch.Activated:Connect(function()

	if punch.Enabled == true and equipped == true then

		loadswing:Play()
		local invoke = remote:InvokeServer("Swing")

		if invoke == true then

			loadswing.KeyframeReached:Connect(function(name)

				if name == "Sound" then

					sound:FireServer()

				end

			end)

		end

	end

end)

This part could be remove the tag before the player that died can register the kill