Hello!
In the sell Item area in my script I have made it so, it makes a variable called tool and that is set to the player’s backpack:findfirstchild(Item) – will be explained from the script
And then I did
if tool then
tool:Destroy()
Item = 0
ItemCost = 0
else
print("Tool could not be found")
end
But it prints out:
“Tool could not be found”
I have no idea why this happens.
Full script:
player = game:GetService("Players").PlayerAdded:Wait()
local RepliStorage = game:GetService("ReplicatedStorage")
local items = {
{name = "Newspaper", weight = 20, cost = 3},
{name = "Rat", weight = 20, cost = 2},
{name = "Broken Furniture", weight = 2, cost = 70},
{name = "Toy", weight = 13, cost = 8},
{name = "Bottle", weight = 20, cost = 3},
{name = "Shoes", weight = 7, cost = 13},
{name = "Monitor", weight = 2, cost = 85},
{name = "Clothes", weight = 13, cost = 10},
{name = "Scrap Metal", weight = 20, cost = 2},
{name = "Cardboard", weight = 20, cost = 4},
{name = "Book", weight = 20, cost = 4},
{name = "Plastic Container", weight = 13, cost = 7},
{name = "Food Scraps", weight = 13, cost = 13},
{name = "Hammer", weight = 7, cost = 30},
{name = "Can", weight = 20, cost = 3},
{name = "Magazines", weight = 13, cost = 12},
{name = "Dishes", weight = 7, cost = 27},
{name = "Batteries", weight = 7, cost = 25},
{name = "Desk", weight = 2, cost = 115},
{name = "Light", weight = 2, cost = 43},
{name = "PC", weight = 1, cost = 468},
{name = "Fridge", weight = 1, cost = 286},
{name = "Microwave", weight = 1, cost = 286},
{name = "Paint Brush", weight = 13, cost = 11},
{name = "Paint Can", weight = 13, cost = 7},
}
local function getRandomItemInfo()
local totalWeight = 0
for _, item in items do
totalWeight = totalWeight + item.weight
end
local randomWeight = math.random(1, totalWeight)
local currentWeight = 0
for _, item in items do
currentWeight = currentWeight + item.weight
if randomWeight <= currentWeight then
return item.name, item.cost
end
end
end
Item = 0
ItemCost = 0
local function onProximityPromptTriggered(plr)
local selectedItem, selectedCost = getRandomItemInfo()
print("Player received: " .. selectedItem)
print(selectedItem.. " is worth: " .. selectedCost)
if RepliStorage:FindFirstChild(selectedItem) then
local tool = RepliStorage.ItemAssets:FindFirstChild(selectedItem):Clone()
tool.Parent = plr.Backpack
Item = selectedItem
ItemCost = selectedCost
else
print(selectedItem.. " is not found in ReplicatedStorage")
local tool = Instance.new("Tool")
tool.Parent = plr.Backpack
tool.Name = selectedItem
local handle = Instance.new("Part")
handle.Parent = tool
handle.Name = "Handle"
handle.Size = Vector3.new(1,1,1)
Item = selectedItem
ItemCost = selectedCost
end
end
local dumpster = script.Parent
RepliStorage.Remotes.Dumpster1.OnServerEvent:Connect(function(plr)
onProximityPromptTriggered(plr)
end)
-- Sell Item
local SellNPCRemote = RepliStorage.Remotes.SellNPC
local NPCProxPromt = workspace.OminusOfficesMap.SellNPC["Hobo Henry"].Torso.ProximityPrompt
SellNPCRemote.OnServerEvent:Connect(function(plr)
if Item ~= 0 and ItemCost ~= 0 then
plr.leaderstats.Credits.Value = plr.leaderstats.Credits.Value + ItemCost
local tool = plr.Backpack:FindFirstChild(Item)
if tool then
tool:Destroy()
Item = 0
ItemCost = 0
else
print("Tool could not be found")
end
else
print("No item")
end
end)
Look at the bottom of the script for the Sell Item area of the script.
The Item and ItemCost variables are between the GetRandomItemInfo and onProximityPromptTriggered functions.