Why doesn't this work?

hello so basically i got this script:

local plr = game:GetService("Players").PlayerAdded:Connect(function(plr)
local leadestats = plr:FindFirstChild("leaderstats")
local goldStats = leadestats and leadestats:FindFirstChild("Gold")
	script.Parent.TriggerEnded:Connect(function()
		if goldStats then
			goldStats.Value -= 300
			script.Parent.Parent:Destroy()
		else
			print("you cant buy this lol")
		end
	end)
end)

and it worked before but now for some reason it prints “you cant buy this lol”

You may want to try plr:WaitForChild("leaderstats") to give it time to load. Same thing for goldStats.

game:GetService("Players").PlayerAdded:Connect(function(plr)
local leaderstats = plr:WaitForChild("leaderstats")
local goldStats = leadestats:WaitForChild("Gold")
-- if goldstats or leaderstats dont exist or will not exist this part will not run so you dont have to worry
	script.Parent.TriggerEnded:Connect(function()
		goldStats.Value -= 300
		script.Parent.Parent:Destroy()
	end)
end)