Index nil with leaderstats

Hey there!

I am currently making a shop system but for some reason the server cant get the players leaderstats. I have tried many solutions such as WaitForChild and FinFirstChild but for both those arguments i got index nil with FindFirstChild/WaitForChild.

The code is in a server script within a button, here is the script:

local serverStore = game:GetService("ServerStorage")
local repStore = game:GetService("ReplicatedStorage")

script.Parent.MouseButton1Click:Connect(function(player) -- Im pretty sure this is returning the player's character but not sure
	local leaderstats = player.leaderstats -- Error is ocurring here
		if leaderstats.Credits.Value >= 10 then -- Set to 6000
			local falClone1 = serverStore.Tools.FAL
			local falClone2 = serverStore.Tools.FAL
			falClone1.Parent = player.Backpack
			falClone2.Parent = player.StartGear
			local char = player.Parent -- Same message as below
			repStore.Core.Events.Purchased:FireClient(char) -- If this :Connect function variable is returning the players character than this wont work
		else
			print("Player doesnt have leaderstats, or they havent loaded in properly yet!") -- This wont print
		end
	end)

This is probably a very simple error but i cant find a way to fix it.

Thanks! :slight_smile:

Always helps to read the documentation: see MouseButton1Click. The player is not passed as an argument to MouseButton1Click: in fact it does not fire with any arguments.

The most egregious problem is mixing your server and client code into one script. The most your LocalScript should be doing is handling the click connection and requesting the server to perform a purchase. Key word: request. The client should be making a request, not telling the server that it did make a purchase. Oh and the client can’t see children in ServerStorage.

I trust you might know how to fix your problem based on this explanation?

1 Like

Actually, Server-sided scripts aren’t able to enter Player’s elements such as PlayerGui.

1 Like