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