Why doesn't an NPC give out a badge to all players when killing them?

I have a problem with a script that when an npc die for some reason gives only one player, and not all.
The script:

local Players = game:GetService("Players")
local Humanoid = script.Parent:WaitForChild('Humanoid')

BadgeID = 2124785121

Humanoid:GetPropertyChangedSignal("Health"):Connect(function(value)
	if Humanoid.Health <= 0 then
		for i, v in pairs(game:GetService("Players"):GetPlayers()) do
			BadgeService:AwardBadge(v.UserId, BadgeID)
		end
	end
end)
2 Likes

Those code looks like it’s finding all the players in the server and awarding the badge to them all.

Here, it says that when the humanoid’s health is 0 (or killed) this will fire, which is correct. Then this part; for i, v in pairs(game:GetService(“Players”):GetPlayers()) do
BadgeService:AwardBadge(v.UserId, BadgeID)

This is finding the players using GetPlayers() and then giving it to all of the Players if found. You’ll need to find the player who killed the NPC.

1 Like

I’m just creating my own Tower Defense game, so…

You could try searching on YouTube on how to give a player a badge/reward for killing a NPC. It will give you a visual example that you can modify to your needs.

Some other things I found:

You may also want to search up on the DevForum too for more help. I’ve seen quite a few questions similar to yours that may help you.

1 Like

no, the problem is that only ONE player is given, you need to give EVERYONE a badge

1 Like

like, there are 4 players on the server and when they kill the final boss, then they get a badge

1 Like

Oh sorry about that.

The code looks right, are there any errors?

1 Like

idk, it doesn’t write anything to output

1 Like

изображение_2021-08-07_163310

1 Like

Hmm the only thing I can think of is that the part of the script that awards the badge is only running one time or for only one player. Maybe you could check using prints to see what player was given it and how many times it runs?

idk, especially since I do not know how it is done

this happens only randomly, a random player is given a badge

Instead of doing that, maybe you could do a loop that keeps running and checking for the humanoid’s health to be 0. Then you make a variable that is basically Getting the service Players and GetPlayers(). Once that’s done, you could just get the players UserID and give the badge.

For example:
while wait() do
if humanoid.Health <= 0 then
local players = game.GetService(“Players”):GetPlayers()
BadgeService:AwardBadge(players.UserID, BadgeID)
end
end

(Sorry if the format is wrong)
This is the only thing I can really think of.

ok, ty for help. I hope I will find this problem

1 Like

No problem. Sorry I couldn’t find the solution to help you. I’m not that good with some things. Hopefully you will find the solution soon. If I think of something that may help, I will let you know here.