Issue with leaderstats

When I kill player, he dies twice ( idk why) and then leaderstats brokes

Here is video
robloxapp-20241109-1227564.wmv (4.1 MB)

and 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")

		-- Проверка на смерть
		humanoid.Died:Connect(function()
			-- Если персонаж уже мертв, не выполнять повторно обработку
			if humanoid.Health <= 0 then
				-- Увеличиваем количество смертей
				deaths.Value += 1

				-- Проверяем наличие "creator" у объекта, который причинил смерть
				local tag = humanoid:FindFirstChild("creator")
				if tag and tag.Value then
					local killer = tag.Value

					-- Увеличиваем количество убийств у убийцы
					if killer and killer:IsA("Player") then
						local killerStats = killer:FindFirstChild("leaderstats")
						if killerStats then
							local killerKills = killerStats:FindFirstChild("Kills")
							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

It works properly when i test it, i think its one of your other scripts.

I have encountered a similar problem where i had a script that changed the state of my humanoid, when a dead humanoid’s state type is changed, because the humanoids health is still 0, it goes back to dead instantly which would trigger the leaderstats again.

So make sure that no script is changing the state type after the humanoid is dead.

Okay, but the only problem is that I can’t find the very script that is interfering

It’s most likely the script that trips when you attack someone with a weapon

Do control-shift-F and search up ChangeState, all the scripts that change the state of humanoid should appear

Ah-ha, I found it look

2 Likes

What should I do now when I found them?

You could just add an if statement that checks if humanoids health is above 0

Could you help me with it, please?

This is Haymaker(Weapon)


It would look something like this

if humanoid.Health > 0 then
	-- Change state here
end

Sorry for that code it would be more like this

if humanoid and humanoid.Health > 0 then
	-- Change state here
end

Let me send you my 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 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 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)

1 Like

Just add the health argument to all the if statements that change state

Statement 1

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

Statement 2

wait(2)

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

You just gotta add the

and humanoid.Health > 0

Before the

then

For all parts that change state

1 Like

Remember that since every tool has its own script that changes humanoids state, you would still need to do the same for the other tools, but after that it should work unless there is another problem

Alright, everything is work now, but Kills don’t count idk why

Here is video
robloxapp-20241109-1326584.wmv (3.8 MB)

You could start by adding a bounch of print statements to check where it fails

humanoid.Died:Connect(function()
			-- Если персонаж уже мертв, не выполнять повторно обработку
			if humanoid.Health <= 0 then
				-- Увеличиваем количество смертей
				deaths.Value += 1

				-- Проверяем наличие "creator" у объекта, который причинил смерть
				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)

I just need copy this and replace?

replace up to this point

1 Like