I’m trying to make a script,that’s upon a boss’s death,give its killer a badge,but it won’t find the player from creator tag.Here’s the script:
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local npcc = workspace:WaitForChild("npc")
local badgeId = 50950130779050
local awardBadgeEvent = ReplicatedStorage:FindFirstChild("AwardBadgeEvent")
local function onNPCDeath(npc)
local humanoid = npc:FindFirstChild("Humanoid")
if humanoid then
local creatorTag = humanoid:FindFirstChild("creator")
if creatorTag and creatorTag:IsA("ObjectValue") and creatorTag.Value then
local player = Players:GetPlayerFromCharacter(creatorTag.Value)
if player then
BadgeService:AwardBadge(player.UserId, badgeId)
else
warn("Player not found from creator tag")
end
else
warn("Creator tag not found or invalid")
end
else
warn("Humanoid not found in NPC")
end
end
npcc:WaitForChild("Humanoid").Died:Connect(function()
onNPCDeath(npcc)
end)
In your script, you’re getting the character object from the tag. This would not work if the tag already referenced the player. You can’t call Players:GetPlayerFromCharacter() on a player object.
I’d double check if you’re storing the player’s character in the objectvalue, or the player’s playerobject.
Question I should’ve asked earlier: Why do you need an NPC when rewarding a badge to a player? Is the NPC the boss? If so, then I assume the creator tag is the person who last dealt damage to the boss. You could make it so the damage script of the player (like a sword) changes the creator value upon attacking.
What kind of damage method do the players have? Sword? Gun?
In the script for this weapon or whatever it is, in the section that contains the code that damages the boss, add a few lines that check if the boss’ humanoid’s health is above 0 (to prevent stealing kills) and if it is, set the creator value to the player.
It’s best if you prefabricate it in Roblox Studio as a child of your NPC. Your weapons need to find this value and assign its wielder to it each time it harms the NPC. Only assign to the value while the NPC is alive
add an ObjectValue to the humanoid of the npc named creator
if you don’t want to do this and want to do it with a script, you can use Instance.new() to create an instance and use .Name to change its name and .Parent to parent it to the humanoid