Getting players leaderstats value in function

I have a simple issue: [Attempt to index local player(a nil value)].
Its Line 7 or
[local leaderstats = player:WaitForChild(“leaderstats”)]
The error Fires immediately and not when the function is called

FYI This is a Shop-Script, this is the server-side.

local storage = game:GetService("ReplicatedStorage")
local remote = storage:WaitForChild("ShopFunction")
local Players = game:GetService("Players")

local function ShopBuy(player,Object,Level)
--Variables
local leaderstats = player:WaitForChild("leaderstats")
local gold = leaderstats.Gold.Value
local rank = leaderstats.Rank.Value
local Clevel = leaderstats.CannonLevel.Value
local Slevel = leaderstats.ShipLevel.Value
--Variables
----Ship	
	if Object == "Ship" then	
		if Level == 1 then
			--Level1
			if gold >= 0 and
				rank >= 0 and
				Slevel == 0 then
					gold = gold-0
					Slevel = 1
			end
	---More Stuff like this ^^^^^^^ (This is a shop script)
end
	end

local player
local Object
local Level
remote.OnServerEvent:Connect(ShopBuy(player,Object,Level))

I basically copied from my click detector script but obviously that never worked.

local but = script.Parent.Parent.StartButton

but.ClickDetector.MouseClick:Connect(function(plr)
local leader =  plr:FindFirstChild("leaderstats")
local shipl = leader.ShipLevel
local cannonl =leader.CannonLevel

Any help is appreciated thanks.

2 Likes

This is your issue.

You are passing three nil values to ShopBuy. Additionally, this is not how you connect a function.

remote.OnServerEvent:Connect(ShopBuy)

What you were doing was passing the result of ShopBuy to :Connect, but your script never even got to that because arguments are evaluated before the function call.

1 Like