Basic Leaderstats Issue

Hello, it has been a minute since I have used the forum, and since I have took a shot at scripting so im quite confused. My friend and I are trying to create a simple game in which we implemented KOs and WOs. The guns that we like do not add kills to the leaderboard. Since it has been a while since I have tried any scripting, I dont know where to start.

Currently inside the weapon are 3 scripts. I figure I have to make a creator tag for it but not sure which script to go into nor how to go about it. Below is a weapon for example.

image

Basically, I would like to understand how to make a creator tag that will add kills to the leaderboard for these weapons, and if I should make the code inside one of the three scripts listed above or in its own separate script, under the weapon. Thanks for any and all help.

6 Likes

Could you look through all the scripts, find one that uses :TakeDamage or just subtracts the Humanoid health, and send it here?

The goal is to deal damage, detect if their health is now below 0, and if it is, increment their kill count and increment the person they killed’s wipeout count.

5 Likes

After using the ctrl + f to find it, theres only one match at the top of the code in this photo. I included some of the code below that, but heres a photo of code above incase needed.

There is more code both below and above it though if needed.

1 Like

Could you paste the _defaultDamageCallback code?

1 Like

local function _defaultDamageCallback(system, target, amount, damageType, dealer, hitInfo, damageData)
if target:IsA(“Humanoid”) then
target:TakeDamage(amount)
end
end

function WeaponsSystem.doDamage(target, amount, damageType, dealer, hitInfo, damageData)
if not target or ancestorHasTag(target, “WeaponsSystemIgnore”) then
return
end
if IsServer then
if target:IsA(“Humanoid”) and dealer:IsA(“Player”) and dealer.Character then
local dealerHumanoid = dealer.Character:FindFirstChildOfClass(“Humanoid”)
local targetPlayer = Players:GetPlayerFromCharacter(target.Parent)
if dealerHumanoid and target ~= dealerHumanoid and targetPlayer then
– Trigger the damage indicator
WeaponData:FireClient(targetPlayer, “HitByOtherPlayer”, dealer.Character.HumanoidRootPart.CFrame.Position)
end
end

	-- NOTE:  damageData is a more or less free-form parameter that can be used for passing information from the code that is dealing damage about the cause.
	-- .The most obvious usage is extracting icons from the various weapon types (in which case a weapon instance would likely be passed in)
	-- ..The default weapons pass in that data
	local handler = _damageCallback or _defaultDamageCallback
	handler(WeaponsSystem, target, amount, damageType, dealer, hitInfo, damageData)
end

end

3 Likes
local function _defaultDamageCallback(system, target, amount, damageType, dealer, hitInfo, damageData)
	if target:IsA("Humanoid") then
		target:TakeDamage(amount)
	end
end

function WeaponsSystem.doDamage(target, amount, damageType, dealer, hitInfo, damageData)
	if not target or ancestorHasTag(target, "WeaponsSystemIgnore") then
		return
	end
	if IsServer then
		if target:IsA("Humanoid") and dealer:IsA("Player") and dealer.Character then
			local dealerHumanoid = dealer.Character:FindFirstChildOfClass("Humanoid")
			local targetPlayer = Players:GetPlayerFromCharacter(target.Parent)
			if dealerHumanoid and target ~= dealerHumanoid and targetPlayer then
				-- Trigger the damage indicator
				WeaponData:FireClient(targetPlayer, "HitByOtherPlayer", dealer.Character.HumanoidRootPart.CFrame.Position)
			end
		end

		-- NOTE:  damageData is a more or less free-form parameter that can be used for passing information from the code that is dealing damage about the cause.
		-- .The most obvious usage is extracting icons from the various weapon types (in which case a weapon instance would likely be passed in)
		-- ..The default weapons pass in that data
		local handler = _damageCallback or _defaultDamageCallback
		handler(WeaponsSystem, target, amount, damageType, dealer, hitInfo, damageData)
	end
end

Sorry here is a better version of that.

2 Likes

I’ve added the functionality and some comments. By the way, the :GetState check can be changed to a Health < 0 check, but I believe :GetState should be more accurate.

Code:

function WeaponsSystem.doDamage(target, amount, damageType, dealer, hitInfo, damageData)
	if not target or ancestorHasTag(target, "WeaponsSystemIgnore") then
		return
	end
	if IsServer then
		local targetPlayer = Players:GetPlayerFromCharacter(target.Parent)

		if targetPlayer and target:IsA("Humanoid") and dealer:IsA("Player") and dealer.Character then
			local dealerHumanoid = dealer.Character:FindFirstChildOfClass("Humanoid")
			if dealerHumanoid and target ~= dealerHumanoid then
				-- Trigger the damage indicator
				WeaponData:FireClient(targetPlayer, "HitByOtherPlayer", dealer.Character.HumanoidRootPart.CFrame.Position)
			end
		end

		-- NOTE:  damageData is a more or less free-form parameter that can be used for passing information from the code that is dealing damage about the cause.
		-- .The most obvious usage is extracting icons from the various weapon types (in which case a weapon instance would likely be passed in)
		-- ..The default weapons pass in that data
		local handler = _damageCallback or _defaultDamageCallback
		handler(WeaponsSystem, target, amount, damageType, dealer, hitInfo, damageData)
		
		-- Check if target is dead
		if targetPlayer and target:GetState() == Enum.HumanoidStateType.Dead then
			-- Give dealer a kill
			dealer.leaderstats.Kills.Value += 1
			
			-- Give target a death
			targetPlayer.leaderstats.Death.Value += 1
		end
	end
end
1 Like

So replacing that code with the one you provided should fix the issue?

1 Like

Replacing that function along with changing the value references (if needed) should work.

2 Likes

Nothing has changed, should I try switching the :GetState ?

1 Like

No errors? That’s probably the first thing you should check.

2 Likes

There were not any errors no. At least script related.

1 Like

I’ve changed it to a health check. Could you test this code and tell me what it prints?

Code:

function WeaponsSystem.doDamage(target, amount, damageType, dealer, hitInfo, damageData)
	if not target or ancestorHasTag(target, "WeaponsSystemIgnore") then
		return
	end
	if IsServer then
		local targetPlayer = Players:GetPlayerFromCharacter(target.Parent)

		if targetPlayer and target:IsA("Humanoid") and dealer:IsA("Player") and dealer.Character then
			local dealerHumanoid = dealer.Character:FindFirstChildOfClass("Humanoid")
			if dealerHumanoid and target ~= dealerHumanoid then
				-- Trigger the damage indicator
				WeaponData:FireClient(targetPlayer, "HitByOtherPlayer", dealer.Character.HumanoidRootPart.CFrame.Position)
			end
		end

		-- NOTE:  damageData is a more or less free-form parameter that can be used for passing information from the code that is dealing damage about the cause.
		-- .The most obvious usage is extracting icons from the various weapon types (in which case a weapon instance would likely be passed in)
		-- ..The default weapons pass in that data
		local handler = _damageCallback or _defaultDamageCallback
		handler(WeaponsSystem, target, amount, damageType, dealer, hitInfo, damageData)
		
		print("Health remaining: ".. target.Health)
		-- Check if target is dead
		if targetPlayer and target.Health <= 0 then
			-- Give dealer a kill
			dealer.leaderstats.Kills.Value += 1

			-- Give target a death
			targetPlayer.leaderstats.Death.Value += 1
		end
	end
end
1 Like

After testing the code, no kill was added nor was anything printed.

1 Like

Did the gun even function in the first place? And are you 100% sure that is the only place where it does damage? Also, are you sure you set your output so that it displays everything and not just certain scripts?

2 Likes

What do you mean by function in the first place? In this case I am using an RPG, when I fire it, it either does splash damage on the player or kills the player. And is there anything else I can refer to instead of _defaultDamageCallback to answer your second question?

image

This is also under the weapon, do you need to see any of these?

1 Like

Could you send the model if possible? It would be easier for me to look rather than ask you to send 10+ scripts.

2 Likes

Surely. Sorry for the inconvenience.

1 Like

By the way, that gun pack is just a copy of official Roblox weapons:

I would recommend using the Roblox version, just incase the one you sent me has bad code.

It also wasn’t your fault why these problems happened. It was because there are multiple copies of that code, some of which were overriding each other.

Here’s the fix:

  1. Grab the WeaponsSystems folder from any gun, and drop it into ServerScriptService. Only do this once. No need to delete duplicates either.
    image

  2. Edit the WeaponsSystem module script in that folder, and change the WeaponsSystem.doDamage function to this:

function WeaponsSystem.doDamage(target, amount, damageType, dealer, hitInfo, damageData)
	if not target or ancestorHasTag(target, "WeaponsSystemIgnore") then
		return
	end
	if IsServer then
		local targetPlayer = Players:GetPlayerFromCharacter(target.Parent)

		if targetPlayer and target:IsA("Humanoid") and dealer:IsA("Player") and dealer.Character then
			local dealerHumanoid = dealer.Character:FindFirstChildOfClass("Humanoid")
			if dealerHumanoid and target ~= dealerHumanoid then
				-- Trigger the damage indicator
				WeaponData:FireClient(targetPlayer, "HitByOtherPlayer", dealer.Character.HumanoidRootPart.CFrame.Position)
			end
		end

		-- NOTE:  damageData is a more or less free-form parameter that can be used for passing information from the code that is dealing damage about the cause.
		-- .The most obvious usage is extracting icons from the various weapon types (in which case a weapon instance would likely be passed in)
		-- ..The default weapons pass in that data
		local handler = _damageCallback or _defaultDamageCallback
		handler(WeaponsSystem, target, amount, damageType, dealer, hitInfo, damageData)
		
		-- Check if target is dead
		if targetPlayer and target.Health <= 0 then
			-- Give dealer a kill
			dealer.leaderstats.Kills.Value += 1

			-- Give target a death
			targetPlayer.leaderstats.Death.Value += 1
		end
	end
end

This should work, I’ve tested it on my own.

1 Like

If I use the roblox version of these weapons, will I still have to change the code? I assume still that I would have to move the WeaponsSystem to ServerScriptService still.

1 Like