Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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:

local plr = script.Parent.Parent.Parent.Parent
if plr and plr:FindFirstChild("Owner") then
	plr = plr.Owner.Value
end

script.Parent.Changed:Connect(function(v)
	print("The change of amountHolding is: "..v)
	if script.Parent.Value < 0 then
		plr.leaderstats.Money.Value += math.abs(script.Parent.Value)
		script.Parent.Value = 0
	end
end)

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

1 comment

0

Change it in the script that subtracts from the leadervalue instead of making a new one specifically to handle this case.

2 answers

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

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

local Players = game:GetService('Players')--added player service
local Player = Players.LocalPlayer--gets the player who activated the script
local plr = script.Parent.Parent.Parent.Parent
if plr and plr:FindFirstChild("Owner") then
    plr = plr.Owner.Value
end

script.Parent.Changed:Connect(function(v)
    print("The change of amountHolding is: "..v)
    if script.Parent.Value < 0 then
        Player.leaderstats.Money.Value += math.abs(script.Parent.Value)--Changed it to Player
        script.Parent.Value = 0
    end
end)

Hope this helps!!

1 comment

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

This discussion is preserved as it was. New answers, comments, and votes are closed.