Attempt to index nil with 'leaderstats'

pretty confusing error, i rewrited two scripts and it doesnt work??

local price = 25 -- change the price of the sword

script.Parent.MouseButton1Click:Connect(function(player)
	if player.leaderstats.Points.Value >= price then
		player.leaderstats.Points.Value -= price
		game.ReplicatedStorage.Tools.ValentineSword:Clone().Parent = player.Backpack
	end
end)
1 Like

Maybe try ( :WaitForChild ) for the player to get leaderstats :woozy_face:

Is this a LocalScript?

If it is a LocalScript, then changing the leaderstats locally wont work.
If it’s a ServerScript, don’t use ServerScripts on GuiObjects.

Try something like this.

Local Script (The TextButton)

local price = require(game.ReplicatedStorage.Prices)["ValentineSword")

script.Parent.MouseButton1Click:Connect(function(player)
	if player.leaderstats.Points.Value >= price then
        game.ReplicatedStorage.Purchase:FireServer("ValentineSword")
	end
end)

Server Script (game.ServerScriptService)

local Prices = require(game.ReplicatedStorage.Prices)

game.ReplicatedStorage.Purchase.OnServerEvent(Player,ItemName)
    if Prices[ItemName] and Player:FindFirstChild("leaderstats") then
        local Points = Player.leaderstats

        if ItemName == "ValentineSword" and Points.Value >= Prices[ItemName] then
            game.ReplicatedStorage.Tools.ValentineSword:Clone().Parent = Player.Backpack
            Player.leaderstats.Points.Value -= Prices[ItemName]
        end
    end
end)

Module Script (game.ReplicatedStorage)

local prices = {
    ValentineSword = 25;
}

return prices

table.find only works on arrays, not dictionaries so it would never purchase the weapon as it will always return nil.

1 Like

Fixed, didn’t know about that.