NPC loot script not working

So im trying to make a script that will give the player an item when killing an npc using Lootplan but whenever i try to use it it says “attempt to call a nil value” on line 11.
If you have any suggestions please tell me (and explain bc im new to scripting)
Here’s the script

local LootPlan = require(game.ReplicatedStorage.LootPlan)

local DropPlan = LootPlan.new(“single”)

local Players = game:GetService(“Players”)

DropPlan:AddLoot(“Apple of Healing”,100) --Item i want to drop

DropPlan:AddLoot(“Nothing”,0) --nothing

Players.PlayerAdded:Connect(function(player) --Me trying to describe the player

local DropType = DropPlan:GetRandomLoot()

DropType:Clone().Parent = player.Backpack --Giving the loot

end)

I’ll try but it says that the problem is on line 11 which is the DropType:Clone line

Didn’t work :confused: I looked it up and it says the error means i was trying to call something that doesn’t exist. Any ideas for that?

Ah, you’re testing the LootPlan suggestion I gave earlier, nice!

Yeah, as the first post mentions this is happening because you’re attempting to clone something that doesn’t exist. Ideally you should only be cloning if the loot is not detected as nothing. Additionally, the last value is a weighting not an exact percentage. The higher the number, the rarer. You could even just use a value of 1 and it’d drop 100% of the time if you have no other loots (including Nothing).

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local LootPlan = require(ReplicatedStorage.LootPlan)
local DropPlan = LootPlan.new("single")

-- In this example, there is a 50% chance of getting Apple of Healing
DropPlan:AddLoot("Apple of Healing", 1)
DropPlan:AddLoot("Nothing", 1)

local function playerAdded(player)
    local dropType = DropPlan:GetRandomLoot()
    if dropType ~= "Nothing" then
        -- Assuming you put the tool in ServerStorage
        game:GetService("ServerStorage")[dropType]:Clone().Parent = player.Backpack
    end
end

Players.PlayerAdded:Connect(playerAdded)
for _, player in ipairs(Players:GetPlayers()) do
    playerAdded(player)
end

Remember my advice in the other thread: LootPlan is only meant to handle the chance algorithm for you but it doesn’t actually select tools or anything, it’s all purely data oriented. You need to build on LootPlan with your own scripts to take the data and turn it into actual item drops.

Did you test this out? it still isn’t working for me i think i’m doing something wrong
The script is normal. The Apple of healing is in ServerStorage. I have a tool named nothing. The script is in the npc. There are no error messages. I really hope it’s me being dumb

@colbert2677 I have been trying new scripts but they all have the same problem, there are no errors but the tool still isn’t given. Any ideas? here is the script

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Tools = game.ServerStorage.Tools

local LootPlan = require(ReplicatedStorage.LootPlan)
local DropPlan = LootPlan.new("single")

-- In this example, there is a 50% chance of getting Apple of Healing
DropPlan:AddLoot("Apple of Healing",1)
DropPlan:AddLoot("Nothing",1)

local function playerAdded(player)
    local dropType = DropPlan:GetRandomLoot()
    if dropType ~= "Nothing" then
	    local Item = Tools:FindFirstChild(dropType()):Clone()
	    Item.Parent = player.Backpack
    end
end

Players.PlayerAdded:Connect(playerAdded)
for _, player in ipairs(Players:GetPlayers()) do
	playerAdded(player)
end

Edit: Whenever i change the rarity of the item it says i tried to call a string value on line 15

You don’t need a tool named Nothing. If you used the same code I wrote then there’s a 50% chance of receiving an Apple of Healing and not, assuming you have Apple of Healing in ServerStorage. As for the new code you posted, this is because you wrote dropType(). dropType is a string not a function, so remove those parenthesis. Can’t call a string.

local Item = Tools:FindFirstChild(dropType):Clone()

I seem to have made it work (sorta). Now the player gets the item based on the chances, but it happens when they join and not when the NPC is killed by the player

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Tools = game.ReplicatedStorage.Tools

local LootPlan = require(ReplicatedStorage.LootPlan)
local DropPlan = LootPlan.new("single")

DropPlan:AddLoot("Apple of Healing", 99)
DropPlan:AddLoot("Nothing", 1)


local function playerAdded(player)
    local dropType = DropPlan:GetRandomLoot()
    if dropType ~= "Nothing" then
	     wait(0)
	    Tools[dropType]:Clone().Parent = player.Backpack
    end
end

Players.PlayerAdded:Connect(playerAdded)
for _, player in ipairs(Players:GetPlayers()) do
    playerAdded(player)
end

Also i really do appreciate the time you’re spending to help me

That’s how it’s coded to work. If you want it to drop the item when an NPC dies then you’d have to connect to their Died event, not the PlayerAdded event. :slight_smile: The fix I provided was under the assumption you were testing LootPlan rather than trying to use it in a production game.

Please do refer to the feedback I gave you in the other thread:

Relevant comment:

LootPlan does not handle everything for you, just the algorithm for loot drops. You are responsible for using the results LootPlan generates for you to further polish your system.

You should set up your NPCs first and have a way to track who did damage/killed the NPC first and then you can apply LootPlan to distribute drops to the player(s) who killed the NPC.