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.
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.
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.
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?
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.
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?
Then I’m not sure why it doesn’t work for you, it works for me just fine. If you’re continuing to test in studio, try testing in-game rather than the studio test and/or restarting studio.
Other than that, I guess you could use the Humanoid | Roblox Creator Documentation event but if the died event doesn’t work I don’t see why this would. It might be best to contact someone at Roblox that can better look into the issue.