Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Attempt to index nil with 'leaderstats' Even though they exist?

Asked by
wswswff 39
1 year ago

So i'm making a machine that converts your resource to money at a tick rate of every second. The issue is that when the tick rate is 4 per second, and your starting amount is 30, you get -2. Now, to combat this, I made this script:

01local plr = script.Parent.Parent.Parent.Parent
02if plr and plr:FindFirstChild("Owner") then
03    plr = plr.Owner.Value
04end
05 
06script.Parent.Changed:Connect(function(v)
07    print("The change of amountHolding is: "..v)
08    if script.Parent.Value < 0 then
09        plr.leaderstats.Money.Value += math.abs(script.Parent.Value)
10        script.Parent.Value = 0
11    end
12end)

I've tried everything. If you know why I get the error please answer

0
Change it in the script that subtracts from the leadervalue instead of making a new one specifically to handle this case. blowup999 659 — 1y

2 answers

Log in to vote
0
Answered by 1 year ago

Answer explain more of your setup but looking at the script it might be line 10 the issue. becuase what are you referring to by doing script.Parent.Value = 0 also this script should be in serverscriptservice

Ad
Log in to vote
0
Answered by 1 year ago

Here's the issue why you get that error on line 1 you use plr as an object of some sort not a player. local plr = script.Parent.Parent.Parent.Parent Then on line 9 you use the plr now as a player plr.leaderstats.Money.Value += math.abs(script.Parent.Value) Try this instead

01local Players = game:GetService('Players')--added player service
02local Player = Players.LocalPlayer--gets the player who activated the script
03local plr = script.Parent.Parent.Parent.Parent
04if plr and plr:FindFirstChild("Owner") then
05    plr = plr.Owner.Value
06end
07 
08script.Parent.Changed:Connect(function(v)
09    print("The change of amountHolding is: "..v)
10    if script.Parent.Value < 0 then
11        Player.leaderstats.Money.Value += math.abs(script.Parent.Value)--Changed it to Player
12        script.Parent.Value = 0
13    end
14end)

Hope this helps!!

0
thanks for trying but I still get the same error. It's okay if you can't find out why. No one is gonna notice an extra 2 bucks right? lol wswswff 39 — 1y

Answer this question