Help with item dropping script

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

4 Likes

sorry if the script is not in the box in the message i don’t know how to do it like that

1 Like

add 3 ` at the start like this (" ```` ") (the button should be below the esc button)
then do the same thing at the end

1 Like

you did it here remove it and put it at the start and end of the code

1 Like

like this? (3) local humanoid = script.Parent:FindFirstChildWhichIsA(“Humanoid”) also question, what does the 3 do? sorry to bother you

1 Like

no no add 3 of the `
so ```
30char

1 Like

oh ok thanks ill try that and see if it works

1 Like

thanks man the box for the dev forum message works now

1 Like

no problem to cut off the code box section you can add another ``` at the end like this
(ignore the brackets)
(‘’')

local var = "myvar"

(```)
asdf

1 Like

ok now the message is normal ithink

1 Like

does anything else print?
30char

1 Like

This code works perfectly fine on my end. But, my “CreatorTag” is an ObjectValue. If you are using a different kind of value such as a StringValue, you should instead check in your “OnHealthDamaged” function if there is a player in game.Players with that CreatorTag’s value, instead of checking if the value itself is a player (or just use an ObjectValue to skip this). Feel free to let me know if you have any more questions!

3 Likes

hey man it worked! but just a little question, for most of this script i had to search up scripts on youtube and stuff, do you mind explaining to me what a StringValue and ObjectValue is? sorry to bother you.

1 Like

Yes! StringValues and ObjectValues are objects, meaning they can be inserted into your game via the Explorer window. They are also used quite often when scripting, as they can hold specific values (Ex: String values hold strings/letters, ObjectValues hold directions to an object, etc.).

You can also read the documentation for values below.
StringValue | Documentation - Roblox Creator Hub
ObjectValue | Documentation - Roblox Creator Hub

1 Like