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)
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?
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?
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.
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)
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
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)
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)