Humanoid.Died did not work

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

Did it work once? I see why it didn’t work more than once and thats my bad, here’s the updated script:

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 function playerDied(humanoid, player)
		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

	local humanoid = player.Character:WaitForChild("Humanoid")
	humanoid.Died:connect(function() --When player dies
		playerDied(humanoid, player)
	end)
	
	player.Changed:connect(function(prop) --When a player respawns we want to watch for .Died again
		if prop == "Character" and player.Character ~= nil then
			local humanoid = player.Character.Humanoid
			local plr = player
			local hum = humanoid
			humanoid.Died:connect(function()
				playerDied(hum, plr)
			end)	
		end
	end)
	
	
end)

Just had to add something in so that after a player respawns it would continue to run the died event for their new/respawned character.
Edit: This works perfectly for me using the default/Roblox sword model.

nope this script doesn’t even give player XP

It gives the enemy XP for killing someone with the sword… that is what you wanted, right? Are you seeing any errors? Or what exactly is your issue, because this script works perfectly fine for me in studio.

yes that’s what I wanted, the first I put it there is an error telling me you forget to put an end, but after I put the end, it still doesn’t work.

I’m also teaching my friend how to script right now, so maybe later I’ll try your new script out

The code in the message above works perfectly fine, it just needs to be a ServerScript and placed in ServerScriptService. It should also take the place of your existing ServerScript. This will be my 3rd time testing it with an in-studio play test so I know it is working code.

nope, it doesn’t work, Idk why

I put it inside of Server Script Storage, and it is a script

Can you send an image of the script and it’s location from the explorer? The script does work, there must be an error on your end. Have you made any changes to the default Roblox sword script?

not rlly, I just change the seconds that the tag will last for

I don’t think there is any error on my game, since i was just a baseplate and I’m just testing on that game

I have had issues with humanoid.died for forever, my advice is to just check if the health of the enemy is 0 and then give the XP. The result is the same.

The game for some reason can’t even detect that the enemy is dead

I also script that if the enemy’s is lower than 0, it will change back to 0

Try readding the sword from the toolbox and try again. If you’re testing in game make sure you’ve properly published to the place you’re testing in. Edit; and can you please send the pictures of the location?