Error getting the leaderstats

Hello, I am trying to make a tycoon dropper for practice and I have run into a problem. It is erroring on the part where it is supposed to give out the points, it gives me an error that says “attempt to index nil with ‘leaderstats’”. I have tried a few different ways to define the player but it still gives me the error.

Script:

while true do
	wait(2)
	local part = Instance.new("Part", game.Workspace)
	part.Position = Vector3.new(-75.997, 6.157, 30.842)
	part.Size = Vector3.new(1.198, 0.467, 1.287)
	part.Color = Color3.fromRGB(27, 42, 53)
	part.Anchored = false
	
	local Pvalue = Instance.new("IntValue")
	Pvalue.Value = 10
	Pvalue.Parent = part
	
	
	local plr = game.Players.LocalPlayer
	wait(5)
	plr.leaderstats.Points.Value = plr.leaderstats.Points.Value + Pvalue
	end

Is this code on a server script…? If it is, you can’t get the LocalPlayer that way

No, it’s in the dropper in the workspace.

I mean the script you sent, there’s 3 types of scripts:

  • Server

  • Local (User Icon)

  • Module (Setting Icon)

I’m not referring to where the script is, I’m wondering what type it is :thinking:

Oh sorry, It’s a server script.

Yeah that explains it

You can’t define the plr like that, if it’s a server script

How exactly does the dropper work? Do you touch a button which dispenses the amount of money you that’s earned from the parts?

2 Likes

No, It’s an automatic dropper and I put an integer value that contains the money’s value in each part that drops.

You’d need to reference the plr variable a different way if the money is automatically earned :thinking:

Does the Dropper become visible after you touch a button? Or is it something else?

That’s the problem I don’t know how to reference the player in another way. Also, no I used an instance function to create the parts so they drop under the dropper. Anyway, is there any other way to reference the player and their leaderstats?

A couple ways I could think of

  • Make it a LocalScript, but the only issue is that everything will be client-sided only

  • Use a PlayerAdded event to get the player that way

The thing is, if you’re not using Touched events it’s a lot more difficult to define the Player which results in a bit more alternate ways to figure out

Why not make this function for each player when they join??

--//PlayerJoinFunction
local function PlayerJoined(plr)
	wait(2)
	local part = Instance.new("Part", workspace)
	--//I'd get each players dropper and assign it there.
	part.Position = Vector3.new(-75.997, 6.157, 30.842)
	part.Size = Vector3.new(1.198, 0.467, 1.287)
	part.Color = Color3.fromRGB(27, 42, 53)
	part.Anchored = false
	
	local Pvalue = Instance.new("IntValue")
	Pvalue.Value = 10
	Pvalue.Parent = part
	
	wait(5)
	plr.leaderstats.Points.Value = plr.leaderstats.Points.Value + Pvalue
end

--//Assign Function
game.Players.PlayerAdded:Connect(PlayerJoined)

Or am I missing something here? (which I highly bet I am) :thinking:

Or, you can do…

--//Make code run whenever you want to for all players....
for i, plr in pairs(game.Players:GetPlayers()) do
	local part = Instance.new("Part", workspace)
	--//I'd get each players dropper and assign it there.
	part.Position = Vector3.new(-75.997, 6.157, 30.842)
	part.Size = Vector3.new(1.198, 0.467, 1.287)
	part.Color = Color3.fromRGB(27, 42, 53)
	part.Anchored = false
	
	local Pvalue = Instance.new("IntValue")
	Pvalue.Value = 10
	Pvalue.Parent = part
	
	wait(5)
	plr.leaderstats.Points.Value = plr.leaderstats.Points.Value + Pvalue
end

Even though that may seem like the solution, that Event will fire every time a Player joins

That means the part will stay in the same position, plus it’s dealt through a while true do loop so I’m not sure what the best scenario would be in this instance when dealing with multiple droppers

I’m not sure what you mean.

Are you/the guy/girl who created this post trying to make a dropper for a tycoon and make a item the part spawn and drop from every x amount of seconds?

The second script didn’t seem to work and the first script only gave the money once.

Because it isn’t looping.

--//PlayerJoinFunction
local function PlayerJoined(plr)
	while true do
		wait(2)
		local part = Instance.new("Part", workspace)
		--//I'd get each players dropper and assign it there.
		part.Position = Vector3.new(-75.997, 6.157, 30.842)
		part.Size = Vector3.new(1.198, 0.467, 1.287)
		part.Color = Color3.fromRGB(27, 42, 53)
		part.Anchored = false
	
		local Pvalue = Instance.new("IntValue")
		Pvalue.Value = 10
		Pvalue.Parent = part
	
		wait(5)
		plr.leaderstats.Points.Value = plr.leaderstats.Points.Value + Pvalue
	end
end

--//Assign Function
game.Players.PlayerAdded:Connect(PlayerJoined)

No, but I’m just assuming that every dropper is individual for each player cause it would make sense to position the parts where they should be (Unless if there’s just 1 main dropper)

I said this above because I assumed that, so you’d want to get each players dropper and position it there, which shouldn’t be to hard

This seems to work, thanks for the help!

Alright, but it is for only one spawner if you want that.

I’m not making a tycoon I just wanted to make this for practice. I am trying to improve my scripting skills.