Humanoid.Died did not work

I’m a little developer that currently making a game. I want to implement an level XP system to my game. I have some basic knowledge of how to make it, but since the first one I made is pretty bad, and I got some suggestions from others, I decided to make a new one. I used Humanoid.Died to give the XP to player since I’m using leaderstats, but for some reason it does not fired. Does anyone knows why it didn’t fired?

Inside of ServerScriptStorage

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	local XP = Instance.new("IntValue",leaderstats)
	XP.Name = "XP"
end)

Inside of an enemy

local Enemy = script.Parent.Humanoid
if Enemy.Health <= 0  then
	Enemy.Health = 0
end

Enemy.Died:Connect(function(player)
	print("It is dead!")
	local PlayerXP = player.leaderstats.XP
	PlayerXP.Value = PlayerXP.Value + 1
end)
1 Like

Is the second script a LocalScript?

If so, this won’t update the PlayerXP value to the server side because of FilteringEnabled. This is just a secure measure to make sure that this value doesn’t replicate to the server in the event of an exploiter.

Read the thing I linked, I made a brief description on this scenario, but there is a lot more too it!

2 Likes

I’m not sure if this is the appropriate way, but why not fetch all the current enemies in a for loop in server (optional), detect .died from server, have a child added event (to detect new enemy spawning)

One thing I just noticed if that you assign the player as a parameter? how do you know which player killed the enemy? there should be some sort of tag on who killed the enemy. You need a kill check

also yea you need to change the values of xp via server you should not allow the client to dictate how much xp one has

1 Like

Since the enemy is in a server, so no, it is not in a localscript

You say its inside the enemy that means the enemy is in workspace

which means it is local

1 Like

I know what tag is but I’m not sure how to make it, I just learned Luau 6 months ago

oof, but I’m using a script tho

1 Like

Humanoid.Died takes no parameters, it doesn’t return anything. What you should do is have an ObjectValue pointing to the player when the enemy dies, then give that player money

1 Like

Correct me if I’m wrong, but being in the Workspace doesn’t necessarily mean it’s local. If they’re using a server script, it’s server sided.

ah ok, but the thing is how to I do that? I just learned Luau 6 months ago so I’ll consider myself as a beginner, or a noob, or whatever you want to call me as

Are you trying to give the enemy the XP for dying or whoever killed them XP for the elimination?

I’m trying to give the player that killed the enemy XP, I know there’s a thing to can make in a script that is called tag, but I’m not sure how to do it.

Right, do you mind showing us the weapon script where the tag is assigned?

for now I’m just using the classic sword in toolbox for testing, but mind if I show you later cuz I need to eat lunch now sorry.

All good, I’ll go check it out and get back with you in a moment.

k back, I heard that the classic sword has a tag in the script right?

Ah I see the tag, I also see how it works when hit an enemy. So right now If I can just get the tag then send those XP to the player then everything is fine right?

1 Like

Yes, the creator tag that is added to the person who’s been hit and has the value of the person’s name who hit them. You just use a single server script (should be placed in ServerScriptService) to check who’s eliminated who and give the enemy the XP.

This should accomplish what you’re looking for:

game.Players.PlayerAdded:Connect(function(player) --Creating leaderstats: as before
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"

local XP = Instance.new("IntValue",leaderstats)
XP.Name = "XP"



while true do --Character load
	if player.Character ~= nil then break
	end
	wait(5)
end

leaderstats.Parent = player

local function getEnemy(humanoid) --Finding the enemy
	local Swordtag = humanoid:FindFirstChild("creator") ---Getting the tag the default sword adds
	if Swordtag ~= nil then
		local enemy = Swordtag.Value
		if enemy.Parent ~= nil then
			return enemy --Getting enemy to add XP in died event
		end
	end
	return nil
end

local humanoid = player.Character:WaitForChild("Humanoid")
humanoid.Died:connect(function() --When player dies
	local killer = getEnemy(humanoid)
	if killer ~= nil then
		local leaderstats = killer:FindFirstChild("leaderstats")
		if leaderstats ~= nil then
			local enemyxp = leaderstats:FindFirstChild("XP")
			if killer ~= player then
				enemyxp.Value = enemyxp.Value + 1 --Giving enemy XP
			else
				--
			end
		end
	end
end)

thx! I’ll try it right now and see if it works

wait it didn’t work, is there’s an error in this script?

1 Like