Item seller from player backpack

hi i have created a script to remove an item from the player backpack and then give the player money but it doesnt work can anyone help me thanks.

local ITEM_NAME = "Pumpkin"
local CURRENCY_NAME = "Cash"
local CURRENCY_PER_ITEM = 10000
local MESSAGE_DURATION = 3

local function onPromptTriggered(prompt, player)
    local backpack = player:FindFirstChildOfClass("Backpack")
    if backpack then
        local item = backpack:FindFirstChild(ITEM_NAME)
        if item and item:IsA("IntValue") and item.Value > 0 then
            item.Value = 0
            local leaderstats = player:FindFirstChild("leaderstats")
            if leaderstats then
                local currency = leaderstats:FindFirstChild(CURRENCY_NAME)
                if currency and currency:IsA("IntValue") then
                    currency.Value = currency.Value + CURRENCY_PER_ITEM
                    local message = Instance.new("Message")
                    message.Text = "You sold your " .. ITEM_NAME .. " for " .. CURRENCY_PER_ITEM .. " " .. CURRENCY_NAME
                    message.Parent = player:WaitForChild("PlayerGui")
                    wait(MESSAGE_DURATION)
                    message:Destroy()
                end
            end
        end
    end
end

local prompt = script.Parent
prompt.Triggered:Connect(function(player)
    onPromptTriggered(prompt, player)
end)
1 Like

Can you re-paste your script with three back ticks like so

```
local ITEM_NAME = “Pumpkin”
local CURRENCY_NAME = “Cash”
local CURRENCY_PER_ITEM = 10000
local MESSAGE_DURATION = 3

local function onPromptTriggered(prompt, player)
local backpack = player:FindFirstChildOfClass(“Backpack”)
```

The result will look much nicer and keep indentation

1 Like

Awesome thank you! Can you explain in more detail what doesn’t work? I think this script will only work if the player has a IntValue Pumpkin in their backpack. Could you show us a screenshot of the pumpkin in the explorer panel?

1 Like

the pumpkin is a tool. when i click the prompt it doesn’t remove the tool and doesn’t give me the Cash idk whats wrong but i think is the backpack or something else can you help me?

Immagine 2023-03-24 185421

local item = backpack:FindFirstChild(ITEM_NAME)
if item and item:IsA("IntValue") and item.Value > 0 then

It’s this line that will always evaluate to false and not run the code below it. item:IsA("Tool")

Can you expand the pumpkin to show it’s children? I’m willing to bet you are actually trying to find an IntValue for cost/price in it’s children.

1 Like

Immagine 2023-03-24 190926

So the Pumpkin does not have a price/cost IntValue as a child, are you using Attributes for this? How have you set the Pumpkin’s price?

the price is in the script i haven’t set any values in the pumpkin

OK! then you should just remove that if statement like so, and make sure you destroy the item when sold.

local ITEM_NAME = "Pumpkin"
local CURRENCY_NAME = "Cash"
local CURRENCY_PER_ITEM = 10000
local MESSAGE_DURATION = 3

local function onPromptTriggered(prompt, player)
	local backpack = player:FindFirstChildOfClass("Backpack")
	if backpack then
		local item = backpack:FindFirstChild(ITEM_NAME) :: Tool
		local leaderstats = player:FindFirstChild("leaderstats")
		if leaderstats then
			local currency = leaderstats:FindFirstChild(CURRENCY_NAME) :: IntValue
			if currency and currency:IsA("IntValue") then
				currency.Value += CURRENCY_PER_ITEM
				item:Destroy()
				local message = Instance.new("Message")
				message.Text = "You sold your " .. ITEM_NAME .. " for " .. CURRENCY_PER_ITEM .. " " .. CURRENCY_NAME
				message.Parent = player:WaitForChild("PlayerGui")
				task.wait(MESSAGE_DURATION)
				message:Destroy()
			end
		end
	end
end

local prompt = script.Parent
prompt.Triggered:Connect(function(player)
    onPromptTriggered(prompt, player)
end)
1 Like
local item = backpack:FindFirstChild(ITEM_NAME)
     
  if item and item:IsA("IntValue") and item.Value > 0 then

If item is a tool then item.Value will be nill, becouse items dont have a propiety called “Value”

1 Like

it doesn’t work if you can remove the message i don’t need it. thx

Why does it not work? I have no idea what happened when you tested this script. Are there errors in the log?

1 Like

idk in the console there isn’t anything i think the best thing is to recreate the script idk

Do you have a “Cash” leaderstat set up? Can you post your script creating leader stats?

Here is a version of the script with warnings instead of just if statements, run this and see what comes up in the output.

local function onPromptTriggered(prompt, player)
	local backpack = player:FindFirstChildOfClass("Backpack")
	if backpack then
		local item = backpack:FindFirstChild(ITEM_NAME) :: Tool
		local leaderstats = player:FindFirstChild("leaderstats")
		if leaderstats then
			local currency = leaderstats:FindFirstChild(CURRENCY_NAME) :: IntValue
			if currency and currency:IsA("IntValue") then
				currency.Value += CURRENCY_PER_ITEM
				item:Destroy()
			else
				warn("Missing leaderstat \"Cash\" or not an int value")
			end
		else
			warn("No leaderstats whatsoever!")
		end
	else
		warn("No player backpack, this is weird!")
	end
end
1 Like
game.Players.PlayerAdded:connect(function(p)
	local stats = Instance.new("IntValue")
	stats.Name = "leaderstats"
	stats.Parent = p
	
	local money = Instance.new("IntValue")
	money.Name = "Cash" -- Change "Money" to anything you want to name it like "Cash"
	money.Value = 200 -- Change the value to how many you want when the player joins the game
	money.Parent = stats
end)

it doesn’t come anything i think is the proximity prompt bugged

i got this script online but it doesn’t work neither

local ting = 0 --debouncer

item = "NameOfItem" --change NameOfItem to the thing your selling

currency = "cash" --change cash to the name of the money you get after you sell

MPI = 1 --change the one to amount of money you get per item

MessageTime = 1 --change this to how long the message stays

function onTouched(hit)

	if ting == 0 then --debounce check
	ting = 1 --activate debounce
	check = hit.Parent:FindFirstChild("Humanoid") --Find the human that touched the button

	if check ~= nil then --If a human is found, then

		local user = game.Players:GetPlayerFromCharacter(hit.Parent) --get player from touching human
		local stats = user:findFirstChild("leaderstats") --Find moneyholder

		if stats ~= nil then --If moneyholder exists then
			local Item = stats:findFirstChild(item) --Item Your Selling 
			local money = stats:findFirstChild(currency)
			if Item.Value > 0  then --makes sure it's greater than 0
                        amnt. = Item.Value*MPI  --finds amount to give
			Item.Value  = Item.Value - Item.Value --sells item..
			wood.Value = wood.Value + amnt.  --gives money
			wait(.5) --wait-time before button works 

local message = Instance.new("Message") --makes message
message.Text = "You Sold Your Blanks For Blank"  --fill in the blanks
message.Parent = game.Players:playerFromCharacter(hit.Parent)
wait(MessageTime)

message:Remove ()  --removes message

end 
		end

	end

	ting = 0 --remove debounce
	end

end

script.Parent.Touched:connect(onTouched)

Probably better to figure out how the proximity prompt is bugged. Is your first script in a server side “Script” or a “LocalScript”?

1 Like

it’s a normal script :grinning: :smiley: :smiley: :pray: :pray: :pray: :pray: :pray: :pray: