Npc proximity prompt, sell items

Hi

I have a wood chopping system. Once the wood is chopped, it stacks up on leaderstats under the name of “Wood”.

Now I want to sell the wood to an npc through using a proximity prompt. How do I sell the wood leaderstat and gain 20 money from it?

image

1 Like

I have this at the moment:

local ProximityPrompt = script.Parent
-- Detect when prompt is triggered

ProximityPrompt.Triggered:Connect(function (plr)
	 plr.backpack.wood:destroy()
	then
		Money.Value = Money.Value + 20 
	end)

Please don’t judge, I’m rather new to scripting.

Try this:

local ProximityPrompt = script.Parent
-- Detect when prompt is triggered

ProximityPrompt.Triggered:Connect(function(Player)
     local Wood = Player.Backpack:FindFirstChild("wood") or Player.Character:FindFirstChild("wood") -- Detect whether it's in their backpack or equipped
	 if Wood then -- if it is equipped or it is in their backpack then
          Player.money.Value += 20
          Wood:Destroy()
     end
end)

Sorry, I kept pressing tab being used that it indents but instead it just posted the unfinished post Lol. Let me know if the code works.

1 Like

How I would do it is I would have a dictionary that contains the item name as the key and the value would be the amount of coins someone get if they are holding the item. When the proximity prompt is triggered I would then see if there is a tool inside of the player character who triggered the proximity prompt and if the tool name is inside of the dictionary as a key I would remove this tool from the character and increase a leaderstats (or however the currency system works).

This method would allow you to add more then just one item and would not require many if statements unlike if you where to check if the user has an item via if statements like what @FP_Nation code does (I don’t recommend if you want many items doing what @FP_Nation put because it could get complicated quick with many items (due to the amount of if statements) and in general not super efficient with large amounts of items)

It doesn’t seem to be working. Sorry I forgot to mention that the wood is actually in leader stat and not inventory.

Just to also quickly go over your code that you have put:

That is not a valid/correct way to use an if statement and will cause your code to error. An example of a valid if statement I will leave below.

if 1 + 1 == 2 then
	
end

Although this would increase the value of the number value called “Money” it can be written by just using += rather then getting the value again and then adding a number on.

For example:

if 1+1 == 2 then

Money.Value += 20

end
1 Like

What is the name of the tool and also what is the number value called (also located)? Could very likely be due to either the tool not being named the same then in the code, the number value not being named the same in the code or the number value is not located in the correct area.

Hi, it’s all leaderstats. Meaning the wood is not an actual item in inventory, but a leaderstat.

This part would be incorrect then if the “Money” number value is a leaderstats value. You would need to do Player.leaderstats.Money += 20

image

Yes, but the wood is also a leaderstat.

local ProximityPrompt = script.Parent
-- Detect when prompt is triggered

ProximityPrompt.Triggered:Connect(function(Player)
	local Wood = Player.Backpack:FindFirstChild("Wood") or Player.Character:FindFirstChild("Wood") -- Detect whether it's in their backpack or equipped
	if Wood then -- if it is equipped or it is in their backpack then
		Player.leaderstats.Money += 20
		Wood:Destroy()
	end
end)

It’s all leaderstats.
image

You have incorrectly worded it then. You would not be “holding” the wood as such but have it in leaderstats.

All you would have to do is check if the wood leaderstats value is more then an amount you want them to be able to redeem to money (or if it’s money ever wood they have just check if the wood value does not equals to one then you can redeem the money). You would then just subtract the amount of wood which is redeemed from the wood leaderstats.

Yes, sorry about that. I have readjusted my post.

Hi, I reupdated my post :slight_smile: Perhaps you could take a look?

I fixed it. Here’s the right code.

local ProximityPrompt = script.Parent

ProximityPrompt.Triggered:Connect(function(Player)
	if Player.leaderstats.Wood.Value > 0 then
		Player.leaderstats.Wood.Value -= 1
		Player.leaderstats.Money.Value +=20
	end
end)
1 Like