How to make a dropper in a tycoon subtract a leaderstat?

Hi! I’m making a tycoon in which you make peanut butter but to do it you need peanuts. So i want to make it so that the droppers will subtract from your peanuts.
The problem is I’m new to scripting and it’s my first time making a tycoon so I don’t know how to access a player and their leaderstats without a touch event with hits and humanoids and stuff.
This is my code for the dropper.
local Values = script.Parent.Parent.Parent.Values

if Values.OwnerValue.Value:WaitForChild(“leaderstats”).PB.Value >= 1 then
while wait(3) do
local reference = script.Parent:WaitForChild(“reference”)
local drop = game:GetService(“ServerStorage”).PeanutDrop:Clone()
drop.Parent = game.Workspace
drop.Position = reference.Position
drop.Name = “drop”

local pbamount = Instance.new("NumberValue")
pbamount.Parent = drop
pbamount.Name = "pbAmount"
pbamount.Value = 1
Values.OwnerValue.Value:WaitForChild("leaderstats").PB.Value -= 1

end

end

**
Btw I accessed the player through a object value set when you claim the tycoon.(it was a tutorial by GamerM8

I couldnt find anybody doing this so idk what to do. I thought of making the dropper have a map covering invisible block that gets the player.

Ps

is supposed to be one line in code. I just accidentaly left it out of the post and added it in so its like that.

local Values = script.Parent.Parent.Parent.Values

if Values.OwnerValue.Value:WaitForChild(“leaderstats”).PB.Value >= 1 then
while wait(3) do
local reference = script.Parent:WaitForChild(“reference”)
local drop = game:GetService(“ServerStorage”).PeanutDrop:Clone()
drop.Parent = game.Workspace
drop.Position = reference.Position
drop.Name = “drop”


	local pbamount = Instance.new("NumberValue")
	pbamount.Parent = drop
	pbamount.Name = "pbAmount"
	pbamount.Value = 1
    Values.OwnerValue.Value:WaitForChild("leaderstats").PB.Value = 
    Values.OwnerValue.Value:WaitForChild("leaderstats").PB.Value - 1
end

end

try making like these because it’s hard to read

Try changing this to:

  Values.OwnerValue.Value:WaitForChild("leaderstats").PB.Value -= 1

It won’t fix the code, as far as I can tell, but it’ll neaten up that one line, as “x -= 1” has the same effect as “x = x - 1”.

Ty but do you know any ways to fix the problem?

Is PB the amount of peanuts the player owns?

Its the value of the drops. The more it is the more peanut butter you get

So you’re sutracting the value of the drop by 1 each time?

yeah each drop is worth one. But its only the first dropper

If the dropper’s value is above or equal to 1,

if Values.OwnerValue.Value:WaitForChild(“leaderstats”).PB.Value >= 1 then

Set a loop that cycles every 3 seconds…

while wait(3) do

… which sets up a drop at reference’s location,

local reference = script.Parent:WaitForChild(“reference”)
local drop = game:GetService(“ServerStorage”).PeanutDrop:Clone()
drop.Parent = game.Workspace
drop.Position = reference.Position
drop.Name = “drop”

sets up a NumberValue within the drop,

local pbamount = Instance.new("NumberValue")
	pbamount.Parent = drop
	pbamount.Name = "pbAmount"
	pbamount.Value = 1

and reduces whatever this is by 1.

Values.OwnerValue.Value:WaitForChild("leaderstats").PB.Value = 
    Values.OwnerValue.Value:WaitForChild("leaderstats").PB.Value - 1

I don’t know what that last bit is.
To add on to this, the loop won’t stop, even if that value drops below 0.

PB is the leader stat for how much peanut butter you have. Pbamount is the amount of peanut butter you get from the drop.

There’s the issue. For every tick, you’re losing one peanut butter.

no it only subtracts every three seconds when it drops.

Also, I’d replace this:

with this:

while wait(3) do
if Values.OwnerValue.Value:WaitForChild("leaderstats").[the actual peanut stat].Value >= then
 [code]
end

The issue is the fact you’re subtracting from the peanut butter stat, not the peanut stat.

Thanks for pointing that out, i didnt realize that xd. But it now gives me an error saying im indexing nil with the waitforchild:(“leaderstats”)

Values.OwnerValue.Value is showing up as nil. Check if it exists when the Script runs.

robloxapp-20210830-2250048.wmv (958.6 KB)
ok so i realized that it was showing nil cuz i didnt claim the tycoon. So i claimed it but it still
doesnt spawn anything

Add code between the comments:

local Values = script.Parent.Parent.Parent.Values

-- ADD THIS
repeat
wait()
until Values.OwnerValue -- or whatever sets to the player when they claim
-- END OF ADDITION

while wait(3) do

This will yield the code until the plot is claimed.