How to sell Items in the Backpack

Hello!

I am trying to make a system where if the player holds out a tool and click the screen, it sells it/removes from the backpack and gives the player an x amount of credits.

I have already created the datastore system for the credits.
The tool appears when interacting with a dumpster. (Sounds wierd but the player gets items from the dumpster)
The tool’s name is an item
The item’s names and values/price are located in the script below.

The content in the first table corresponds to the second table. For example the nespaper is worth 3 credits.

If you need more info then ask me!

Script: (name:SellItem

local Items = {
	
	"Newspaper",
	"Rat",
	"Broken furniture",
	"Toy",
	"Bottle",
	"Shoes",
	"Monitor",
	"Clothes",
	"Scrap metal",
	"Cardboard",
	"Book",
	"Plastic container",
	"Food scraps",
	"Hammer",
	"Can",
	"Magazines",
	"Dishes",
	"Batteries",
	"Desk",
	"Chair",
	"Keyboard",
	"Lights",
	"PC",
	"Fridge",
	"Microwave",
	"Paint Brush",
	"Paint Can"

}

local ItemValue = {
	
	3,
	2,
	70,
	8,
	3,
	13,
	85,
	10,
	2,
	4,
	4,
	7,
	13,
	30,
	3,
	12,
	27,
	25,
	115,
	43,
	58,
	43,
	468,
	324,
	286,
	11,
	7,
	
}

DataStore Script: (Name:DataStoreScript)

local DataStoreService = game:GetService("DataStoreService")
local playerDataStore = DataStoreService:GetDataStore("PlayerData")

local function savePlayerData(player)
    local success, errorMessage = pcall(function()
        playerDataStore:SetAsync(player.UserId, player.leaderstats.Credits.Value)
    end)
    if not success then
        warn("Failed to save data for player " .. player.Name .. ": " .. errorMessage)
    end
end

local function loadPlayerData(player)
    local success, data = pcall(function()
        return playerDataStore:GetAsync(player.UserId)
    end)
    if success and data then
        player.leaderstats.Credits.Value = data
    else
        player.leaderstats.Credits.Value = 0
    end
end

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local credits = Instance.new("IntValue")
    credits.Name = "Credits"
    credits.Parent = leaderstats

    loadPlayerData(player)
end)

game.Players.PlayerRemoving:Connect(function(player)
    savePlayerData(player)
end)

GivePlayerToolScript(Name:GiveItem)

local Dumpster = workspace.Dumpster
local ProxPromt = Dumpster:WaitForChild("Main").ProximityPrompt
local SP = game:GetService("StarterPack")

ProxPromt.Triggered:Connect(function(plr)
	local ItemNum = math.random(1,337)
	if ItemNum >1 and ItemNum <200 then
		--print("Common")
		local ItemC = math.random(1,7)
		if ItemC == 1 then
			print("Newspaper, Common")
			local tool = Instance.new("Tool")
			tool.Name = "Newspaper"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		elseif ItemC == 2 then
			print("Rat, Common")
			local tool = Instance.new("Tool")
			tool.Name = "Rat"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		elseif ItemC == 3 then
			print("Bottle, Common")
			local tool = Instance.new("Tool")
			tool.Name = "Bottle"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		elseif ItemC == 4 then
			print("Scrap Metal, Common")
			local tool = Instance.new("Tool")
			tool.Name = "Scrap Metal"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		elseif ItemC == 5 then
			print("Cardboard, Common")
			local tool = Instance.new("Tool")
			tool.Name = "Cardboard"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		elseif ItemC == 6 then
			print("Book, Common")
			local tool = Instance.new("Tool")
			tool.Name = "Book"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		elseif ItemC == 7 then
			print("Can, Common")
			local tool = Instance.new("Tool")
			tool.Name = "Can"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		end
	elseif ItemNum >201 and ItemNum <300 then
		--print("Uncommon")
		local ItemU = math.random(1,7)
		if ItemU == 1 then
			print("Toy, Uncommon")
			local tool = Instance.new("Tool")
			tool.Name = "Toy"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		elseif ItemU == 2 then
			print("Clothes, Uncommon")
			local tool = Instance.new("Tool")
			tool.Name = "Clothes"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		elseif ItemU == 3 then
			print("Plastic Container, Uncommon")
			local tool = Instance.new("Tool")
			tool.Name = "Plastic Container"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		elseif ItemU == 4 then
			print("Food Scraps, Uncommon")
			local tool = Instance.new("Tool")
			tool.Name = "Food Scraps"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		elseif ItemU == 5 then
			print("Magazine, Uncommon")
			local tool = Instance.new("Tool")
			tool.Name = "Magazine"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		elseif ItemU == 6 then
			print("Paint Brush, Uncommon")
			local tool = Instance.new("Tool")
			tool.Name = "Paint Brush"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		elseif ItemU == 7 then
			print("Paint Can, Uncommon")
			local tool = Instance.new("Tool")
			tool.Name = "Paint Can"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		end
	elseif ItemNum >301 and ItemNum <320 then
		--print("Rare")
		local ItemR = math.random(1,5)
		if ItemR == 1 then
			print("Shoes, Rare")
			local tool = Instance.new("Tool")
			tool.Name = "Shoes"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		elseif ItemR == 2 then
			print("Hammer, Rare")
			local tool = Instance.new("Tool")
			tool.Name = "Hammer"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		elseif ItemR == 3 then
			print("Dishes, Rare")
			local tool = Instance.new("Tool")
			tool.Name = "Dishes"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		elseif ItemR == 4 then
			print("Batteries, Rare")
			local tool = Instance.new("Tool")
			tool.Name = "Batteries"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		elseif ItemR == 5 then
			print("Chair, Rare")
			local tool = Instance.new("Tool")
			tool.Name = "Chair"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		end
	elseif ItemNum >321 and ItemNum <335 then
		--print("Legendary")
		local ItemL = math.random(1,5)
		if ItemL == 1 then
			print("Broken Furniture, LEGENDARY")
			local tool = Instance.new("Tool")
			tool.Name = "Broken Furniture"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		elseif ItemL == 2 then
			print("Monitor, LEGENDARY")
			local tool = Instance.new("Tool")
			tool.Name = "Monitor"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		elseif ItemL == 3 then
			print("Desk, LEGENDARY")
			local tool = Instance.new("Tool")
			tool.Name = "Desk"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		elseif ItemL == 4 then
			print("Keyboard, LEGENDARY")
			local tool = Instance.new("Tool")
			tool.Name = "Keyboard"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		elseif ItemL == 5 then
			print("Light, LEGENDARY")
			local tool = Instance.new("Tool")
			tool.Name = "Light"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		end
	elseif ItemNum >334 and ItemNum <337 then
		--print("MYTHICAL")
		local ItemM = math.random(1,3)
		if ItemM == 1 then
			print("PC, MYTHICAL!!!")
			local tool = Instance.new("Tool")
			tool.Name = "PC"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		elseif ItemM == 2 then
			print("Fridge, MYTHICAL!!!")
			local tool = Instance.new("Tool")
			tool.Name = "Fridge"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		elseif ItemM == 3 then
			print("Microwave, MYTHICAL!!!")
			local tool = Instance.new("Tool")
			tool.Name = "Microwave"
			tool.Parent = plr.Backpack
			local Item = Instance.new("Part")
			Item.Name = "Handle"
			Item.Parent = tool
		end
	end
end)

Also if you are reffering to a script please call it by the name.
Also edit the “SellItem” script

2 Likes

PLEASE MAKE THE SECOND SCRIPT USE A FUNCTION

local function CreateTool(name)
 local tool = Instance.new("Tool")
 tool.Name = name
 tool.Parent = plr.Backpack

 local Item = Instance.new("Part")
 Item.Name = "Handle"
 Item.Parent = tool

 return tool
end

also you can hold all of the tools in a table

--for example
Items = {
 Common = {'Newspaper', 'Rat',} --etc, etc..
}

local ItemName = items.Common[math.random(1, #items.Common)]
local tool = CreateTool(ItemName) --assuming you also have createtool still defined

also your items table can look like this

Items = {
  ['Newspaper'] = 3 -- 3 being the price
}
2 Likes

By the second script do you mean the datastorescript?

1 Like

third* i meant the script that creates the items

1 Like

also let me suggest a weighted luck system: Weighted Chance System

3 Likes

How would i need to call the function?

1 Like

this looks like a chance system
read the post i linked to get more understanding, i can still create a function for it if needed.

1 Like

It is a chance system. Would it still do the same thing as the weighted chance system?

1 Like

it would do it better and be easier to implement

if you give the percentages i can write a function

1 Like

Common 1/5 – 20%

Uncommon 1/10 – 10%

Rare 1/50 – 2%

Legendary 1/300 – 0.3%

Mythical 1/1000 – 0.1%

local Odds = {
	Common = 20,
	Uncommon = 10,
	Rare = 2,
	Legendary = .3,
	Mythical = .1
}

local function GetRandomRarity()
	local weight = 0 
	
	for _, chance in Odds do
		weight += (chance * 10)
	end
	
	local rng = math.random(1, weight)
	
	weight = 0
	
	for rarity, chance in Odds do
		weight += (chance * 10)
		
		if weight >= rng then
			return rarity
		end
	end
end
2 Likes

Now make it so it makes a tool in the backpack

No, I’m not going to do that, you need to write that on your own.

I’ve given you every tip/function you need to make your code better, I’m not writing your scripts.

heres a way you can do this, not writing the full script though:

local Rarity = GetRandomRarity()
local ItemName = Items[Rarity][math.random(1, #Items[Rarity])]
local tool = CreateTool(ItemName)
1 Like

ok. Can you give me tips on how I would need to sell the tools though?

1 Like

Yeah

First, when the player holds the tool you can use .Activated to check if the player clicks with a tool out, if your trying to make it detect on ui you should know how to do that

Then, you need to find the tool (in the player and table in the first script) and price and then destroy the tool from the player

Finally, give the player currency

2 Likes