Attempt to index nil with 'leaderstats'

I just want to make this button increment the “Money” stat by 1, however I always get
attempt to index nil with ‘leaderstats’

Here is my script:

local MoneyButton = script.Parent

MoneyButton.MouseButton1Click:Connect(function()

moneyStat = game.Players.LocalPlayer.leaderstats.Money

moneyStat.Value = moneyStat.Value + 1

end)

leaderstats is inside the LocalPlayer, and Money is inside leaderstats.
Would also be very helpful if I get an explanation on how to fix indexing nil with other objects too as it happens with a lot of things I do.

2 Likes

Start a game and show me whats inside your player.

1 Like

The problem lies in this line game.Players.LocalPlayer, assuming you are running this code from a script. LocalPlayer is only accessible from the client side.

1 Like

the Money Value is nil?

the Attempt to index nil with '<instance-name>' means something doesn’t exist and you need to create that something for it to stop occurring

1 Like

“attempt to index nil with ‘leaderstats’” means LocalPlayer doesnt exist

1 Like

uhh maybe that script is not a localscript?

1 Like

Made a quick system for what you have asked, simple enough…
Made it a downloadable file because I have other stuff to do,
and makes it easier to get it fully working on your end.

And it would be in your best interest to look over the script so that you can getter a better understanding on what is happening when it comes to remote events…

IncreaseMoneyBaseplate.rbxl (43.6 KB)

It means the money value isn’t existing. Go into the game and go to the player then the folder then check the leaderstats folder to check if the money value is there.

Believe that you are confused and new to scripting so let me help you out a little.

Not sure how you have setup everything in your game but heres how I would do what I think you are trying to do.

Leaderboard/Leaderstats script

Copy paste the code below into a Script inside of ServerScriptService
(i would recommend using ProfileService for money instead BUT since you are new (i think) to scripting we will keep it quick and simple as ProfileService can be messy)

-- // Script Made by DARKMASTER6906 // --

--// Services
local DataStoreService = game:GetService("DataStoreService") 
local Players = game:getService("Players")

--// DataStore
local CurrencyData = DataStoreService:GetDataStore("CurrencyData")

--// Configuration
local CurrencyName = "Money" 
local CStartingValue = 0

--// Getting Data - Getting the Datastore
local function loadData(player)
	local UserData
	local success, errMsg = pcall(function() --Uses pcall for when DataStore go down, prevents script errors.
		UserData = CurrencyData:GetAsync(player.UserId)
	end)

	if success == false then --Protects old data when the DataStores go down
		local doNotSave = Instance.new("Folder")
		doNotSave.Name = "DoNotSave"
		doNotSave.Parent = player
		--When the player leaves the game with this Folder inside of them their data will not save/overwrite old data
	else
		print("Data load successful | User: "..player.Name)
	end

	local leaderstats = Instance.new("Folder")  --Folder to hold leaderboard statistics, etc. Folder goes inside player
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Currency = Instance.new("IntValue")
	Currency.Name = CurrencyName
	Currency.Value = UserData or CStartingValue 
	Currency.Parent = leaderstats
end

--// Saving - Saving Datastore
local function saveData(player) --Saves data when player leaves the game
	local CSavingPath = player.leaderstats:FindFirstChild(CurrencyName)

	if player:FindFirstChild("DoNotSave") then --Detects if player's data is savable when old data cannot be loaded
		warn("Player data was not saved to avoid data loss.")
	else
		CurrencyData:SetAsync(player.UserId, CSavingPath.Value) ----If the data was loaded successfully, the data will save
	end
end

--// Connections
Players.PlayerAdded:Connect(loadData)
Players.PlayerRemoving:Connect(saveData)
Server script for giving money

This script must be in ServerScriptService as a Script aswell

NOTE: The remoteevent used in this script is unsecure as I dont have enough info on what this system that you are making is about or how it fully works.
You may learn more about this here.

-- // Script Made by DARKMASTER6906 // --

--// Services
local Players = game:getService("Players")
local RS = game:GetService("ReplicatedStorage")

--// Events
local MoneyEvent = RS:WaitForChild("MoneyEvent")

--// Functions
local function giveMoney(Player)
	--RemoteEvents going from the client to the server can be highly exploited
	--To prevent this from happening you need to use sanity checks to make sure these requests from the client are legit
	--Since I dont know what this script is for or how your game works, i cannot put sanity checks in since I have nothing to check

	local leaderstats = Player.leaderstats
	local MoneyStat = leaderstats and leaderstats:WaitForChild("Money")
	
	MoneyStat += 1
end

--// Connections
MoneyEvent.OnServerEvent:Connect(giveMoney)
LocalScript for getting when the GUI button is activated

This LocalScript needs to go anywhere in your GUI which should be in StarterGui.
From the script you provided I will be assuming the script is under the gui button, this is where im getting the button in this script too so please but it there.

-- // Script Made by DARKMASTER6906 // --

--// Services
local Players = game:getService("Players")
local RS = game:GetService("ReplicatedStorage")

--// Player
local Player = Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()

--// Events
local MoneyEvent = RS:WaitForChild("MoneyEvent")

--// GUI
local MoneyButton = script.Parent

--// Functions
local debounce = false --// debounce, also known as a cooldown, prevents this script from getting spammed, lagging the client and server and possibly breaking
local function onClick()
	if debounce == true then return else --if the debounce is enabled then stop the function and wait for another click
		debounce = true --// enabling the debounce
		
		--now here you can do many things that you like
		--but what im here for is to provide a money giving script 
		--so thats all that ill do here
		MoneyEvent:FireServer()
		
		task.wait(1) --// lenght of the cooldown
		debounce = false --// disabling the debounce
	end
end

--// Connections
MoneyButton.MouseButton1Click:Connect(onClick)

Everything (i think) that you need is provided above. If you need anything else or something breaks and doesnt work dont be scared to reply back!!
If this does work fine and to your needs please mark it as the solution.

might be a few typos in here that i didnt see before sending, im dyslexic so just point them out and i will fix them!

you use WaitForChild is solution