Grabbing leaderstats nil?

Hello, so I have this script that I want to give the player + 50 cash, however it gives me an error

ServerScriptService.ProductShopServer:12: attempt to index nil with 'FindFirstChild' - Server - ProductShopServer:12

local marketplaceService = game:GetService("MarketplaceService")
local players = game:GetService("Players")

local serverStorage = game:GetService("ServerStorage")

local Fifty = 1681205544



local function GiveFifty(player)
	
	player:FindFirstChild("leaderstats").Cash.Value = player:FindFirstChild("leaderstats").Cash.Value + 50
	
end



marketplaceService.PromptProductPurchaseFinished:Connect(function(player, Fifty, wasPurchased)
	if wasPurchased then
		GiveFifty()
	end
end)

How can I fix this?

1 Like

You aren’t passing player to Give Fifty

Sorry im not to sure what that means, I really only know the basics of scripting lol

Try this instead

local marketplaceService = game:GetService("MarketplaceService")
local players = game:GetService("Players")

local serverStorage = game:GetService("ServerStorage")

local Fifty = 1681205544



local function GiveFifty(player)
	
	player:FindFirstChild("leaderstats").Cash.Value = player:FindFirstChild("leaderstats").Cash.Value + 50
	
end



marketplaceService.PromptProductPurchaseFinished:Connect(function(player, Fifty, wasPurchased)
	if wasPurchased then
		GiveFifty(player)
	end
end)

Still getting this error ServerScriptService.ProductShopServer:12: attempt to index number with 'FindFirstChild' - Server - ProductShopServer:12

Try this

local marketplaceService = game:GetService("MarketplaceService")
local players = game:GetService("Players")

local serverStorage = game:GetService("ServerStorage")

local Fifty = 1681205544



local function GiveFifty(player)
	
	player:FindFirstChild("leaderstats").Cash.Value = player:FindFirstChild("leaderstats").Cash.Value + 50
	
end



marketplaceService.PromptProductPurchaseFinished:Connect(function(playerID, Fifty, wasPurchased)
  local player = players:GetPlayerByUserId(playerID)
	if wasPurchased then
		GiveFifty(player)
	end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.