Attempt To Index Nil With leaderstats?

Problem: This Gui Wont Let Me Buy Tool With Cash On Leaderstats

Error Message:
image

local price = 5000 -- Your price goes here.
local tool = game.ServerStorage:WaitForChild("AKS-74U") -- Put the name of your tool here.

local function buy(player)
	local money = player.leaderstats:WaitForChild("cash")

	if money.Value >= price then
		money.Value = money.Value - price
		local a = tool:clone()
		a.Parent = player.Backpack	
	end
end
script.Parent.MouseButton1Click:Connect(buy)

also using wait for child does not work

When you get the attempt to index nil with “x” error, that means whatever you’re trying to access does not exist. In this case, “cash” does not exist. Check to see if “cash” is parented correctly.

Well not really, ‘player’ is the thing that does not exist. MouseButton1Click does not have any parameters and should be done on the client anyways, where you use LocalPlayer.

I think remote events are needed here.

So put the MouseButton1Click function on the client, and fire a remote event to the server.

On line 5, “player” appears to be an unknown variable. Or is this just a sample and it’s defined in the un-shown part of the script?

You’ll have to reference a LocalScript, a Script, and a RemoteEvent in order to properly accomplish this

In relation to what @ZOMBEIVEroblox said, a RemoteEvent is in simple terms: A way for clients & servers to send data over each other, and vice versa

You’ll have to create a RemoteEvent inside ReplicatedStorage, as that’s accessible by both the client and server itself

You can use game.Players.LocalPlayer to reference the Player that way instead in the script shown in the OP, the MouseButton1Click Event has no parameters:

local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local Player = game.Players.LocalPlayer

local function buy()
	Event:FireServer()
end

script.Parent.MouseButton1Click:Connect(buy)

Next, you’d want to create a Script (Preferably) inside ServerScriptService:

local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local Tool = game.ServerStorage:WaitForChild("AKS-74U")
local Price = 5000

Event.OnServerEvent:Connect(function(Player)
    local money = player.leaderstats:WaitForChild("cash")

    if money.Value >= price then
        money.value -= price

        local ToolClone = Tool:Clone()
        ToolClone.Parent = Player.Backpack
    end
end)

FireServer() would fire a request from the client side, to the server and OnServerEvent would be receiving those said “requests” when they’re fired

2 Likes

Ok, you do NOT need remote events like the others said. You only need to get the player through the script like this:

local player = script.Parent.Parent.Parent.Parent.Parent

MouseButton1Click should be done on the client though.

I think @johncena12345678953 made a shop gui that, regardless of the type of script, will not get the player with MouseButton1Click, remote events are the best option.

It would be very good if MouseButton1Click gave the player, everything would be easier, but I think they never will

1 Like

That’s not really good practice though to be referencing so much script.Parent's just to get the Player alone through a Server Script, plus UI Events should be properly handled on the client

Not to mention that sometimes, some Events won’t work if they’re handled on the server-side

If you have like a bunch more weapons you want to confirm, using RemoteEvents can be an important key aspect

Example, passing a string parameter onto the server:

local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local Player = game.Players.LocalPlayer

local function buy()
	Event:FireServer("AKS-74U")
end

script.Parent.MouseButton1Click:Connect(buy)
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

Event.OnServerEvent:Connect(function(Player, ToolName)
    local Price = 0
    local money = player.leaderstats:WaitForChild("cash")
    local ToolCheck = game.ServerStorage:FindFirstChild(ToolName)

    if ToolCheck then --Checking if there's a Valid Tool inside ServerStorage
        if ToolCheck.Name == "AKS-74U" then
            Price = 5000
        elseif ToolCheck.Name == "OtherToolHere" then
            Price = 2500
        end

        if money.Value >= Price then
            money.Value -= Price

            local ToolClone = ToolCheck:Clone()
            ToolClone.Parent = Player.Backpack
        end
    end

end)

(I accidentally hit enter, why do you do this to me Forum)

Besides that it is more secure and not so easy to hack :wink:

1 Like

You should probably handle buttons on the client. I use this to get the player from a serverscript, I don’t know if it’s a good method to use.

script:FindFirstAncestorOfClass('Player')