Hello, I’m trying to make a npc for my game drop an item once he is killed. I want it to drop only to players who dealt damage to it and have a 30% chance. this is the script – Define the humanoid
local damageDealers = {}
local function onHealthChanged()
local creatorTag = humanoid and humanoid:FindFirstChild("creator")
if creatorTag then
local creator = creatorTag.Value
if creator and creator:IsA("Player") then
damageDealers[creator] = true
print("Player " .. creator.Name .. " has dealt damage.")
end
end
end
local function onDeath()
print("King Of The Dark Empire has died.")
local foundDamageDealer = false
for player, _ in pairs(damageDealers) do
foundDamageDealer = true
local chance = math.random(1, 100)
print("Checking chance for player " .. player.Name)
if chance <= 30 then
print("Player " .. player.Name .. " wins the Adurite Crown.")
local AduriteCrown = game.ServerStorage:FindFirstChild("AduriteCrown")
if AduriteCrown then
local AduriteCrownClone = AduriteCrown:Clone()
AduriteCrownClone.Parent = player.Backpack
else
warn("AduriteCrown model not found in ServerStorage.")
end
else
print("Player " .. player.Name .. " did not win the Adurite Crown.")
end
end
-- Handle case where no damage dealers were found
if not foundDamageDealer then
print("No players dealt damage to King Of The Dark Empire.")
end
end
-- Function to handle tool interactions and damage
local function handleTool(tool)
local player = game.Players:GetPlayerFromCharacter(tool.Parent)
if player then
tool.Activated:Connect(function()
-- Simulate damage to humanoid
if humanoid and humanoid.Parent then
humanoid:TakeDamage(10) -- Adjust damage amount as needed
end
end)
end
end
-- Monitor for new players and their tools
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
for _, child in ipairs(character:GetChildren()) do
if child:IsA("Tool") then
handleTool(child)
end
end
end)
end)
-- Check existing players and their tools
for _, player in ipairs(game.Players:GetPlayers()) do
if player.Character then
for _, child in ipairs(player.Character:GetChildren()) do
if child:IsA("Tool") then
handleTool(child)
end
end
end
end
-- Connect events only if humanoid is found
if humanoid then
humanoid.HealthChanged:Connect(onHealthChanged)
humanoid.Died:Connect(onDeath)
else
warn("Humanoid not found for King Of The Dark Empire.")
end
It always prints “No players dealt damage to King Of The Dark Empire.” so I am very confused on what to do. I tried looking over the script but I could finding nothing that I think I should change. Someone please help